首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Kernighan & Ritchie代码示例混淆

Kernighan & Ritchie代码示例混淆
EN

Stack Overflow用户
提问于 2014-12-01 06:30:13
回答 5查看 189关注 0票数 0

在这个代码示例中提到第二个'c = getchar()‘有什么原因吗?

代码语言:javascript
运行
复制
#include <stdio.h>
/* copy input to output; 1st version */  

int main(void) {

    int c;

    c = getchar();
    while (c != EOF) {
        putchar(c);
        c = getchar(); // <-- I mean this one.
    }
    return 0;
}
EN

回答 5

Stack Overflow用户

发布于 2014-12-01 06:33:14

是的,所以它不会破坏EOF。

它读取第一个字符,检查它是否不是EOF,然后putChars它,然后获得另一个字符,回到while循环的顶部,并检查它的not。

票数 1
EN

Stack Overflow用户

发布于 2014-12-01 06:33:47

第二个c = getchar()是读取另一个字符,然后再读取另一个字符,直到EOF满足为止。

票数 1
EN

Stack Overflow用户

发布于 2014-12-01 06:35:18

首先,c = getchar();只能工作一次,但是will循环中的c = getchar();每次都会工作到c != EOF为止。

代码语言:javascript
运行
复制
c = getchar(); // Read value of `c` if `c != EOF` it will enter while loop else it will exit
while (c != EOF) {  // checking condition
   putchar(c);     //printing value of c
   c = getchar(); // again getting new value of c and checking in while loop,  
                    //if condition is true it will continue, else it will exit
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27222853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档