#include<stdio.h>
int main(){
int c = getchar();
while(c != EOF){
putchar(c);
c = getchar();
}
}在上面的代码中,为什么在c变成EOF之后,程序不会自动终止?Reference of the code > Book: K&R's The C Programming Language第二版,第18页
发布于 2017-11-15 13:08:48
getchar()只有在到达文件末尾时才会返回EOF。这里的‘file’是标准输入本身。这可以写成:
#include <stdio.h>
int main()
{
int c;
while ((c = getchar()) != EOF)
{
/*getchar() returns the the next available value which is in the input
buffer*/
putchar(c);
}
}https://stackoverflow.com/questions/47299522
复制相似问题