【发布时间】:2018-04-27 19:22:14
【问题描述】:
#include <stdio.h>
/*Checking whether the value of (getchar() != EOF) is 1,
when not reaching the EOF*/
main() {
int c;
printf("Please enter character:\n");
while (c = getchar() != EOF) {
printf("%d\t", c);
}
printf("%d - at EOF\n", c);
}
我在CLion中运行过这段代码,但是出现了一个问题,第一个printf()中的内容直到我输入了一些字才出现。
有一个例子。
error
^D
Please enter character:
1 1 1 1 1 1 0 - at EOF
我知道它可能是因为我已禁用了run.processes.with.pty在注册表中,因为句子“请输入字符:”选项可用时位于正确的位置。但是如果我不这样做,我就不能使用 Ctrl+D 来发送 EOF。另外,似乎只有当我在字符后的新空行中键入 Ctrl+D 时结果才正确。
平台:Windows 10,工具链:MinGW
顺便说一句,我也试过 Cygwin。同样的问题再次发生。有什么想法吗?
【问题讨论】:
-
因为operator precedence你有:
while (c = (getchar() != EOF)) { -
main()应该是int main(void)。