【发布时间】:2012-06-05 15:18:19
【问题描述】:
可能重复:
Why doesn't getchar() recognise return as EOF in windows console?
我是 C 新手,想弄清楚 EOF 和 getChar() 是如何工作的。
#include <stdio.h>
main()
{
int number = 0;
while(getchar() != EOF)
number++;
printf("%d", number);
}
当我输入一些随机字符时,程序什么也不做,我认为它永远不会退出那个 while 循环。这是为什么?我在 Windows 7 上使用 CodeBlocks。
【问题讨论】:
-
EOF 是文件结束的标记。键入 CTRL+Z 或更改条件以在键盘上输入字符退出循环。
-
因为 EOF 不能用键盘输入。 :)