CLion 中的 EOF 错误
An EOF bug in CLion
#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 选项,因为当该选项可用时,句子 'Please enter character:' 位于正确的位置。但是如果我不这样做,我就不能使用 Ctrl+D 来发送 EOF。此外,似乎只有当我在字符后的新空行中键入 Ctrl+D 时结果才能正确。
平台:Windows10,工具链:MinGW
顺便说一句,我也尝试过 Cygwin。同样的问题又出现了。有什么想法吗?
问题是
c = getchar() != EOF
真的只是
c = (getchar() != EOF)
你想要的是
(c = getchar()) != EOF
如果您使用 c = getchar() != EOF
,许多编译器会生成警告
$ cc -c test.c -Wall -Wextra
test.c:5:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main() {
^~~~
test.c: In function ‘main’:
test.c:9:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
while (c = getchar() != EOF) {
^
这就是建议启用警告的原因。对于新项目,我认为 -Wall -Wextra
是最低限度。
运算符优先级。 @Johnny Mopp
c = getchar() != EOF
和c = (getchar() != EOF)
一样,当然不是OP想要的。
// while (c = getchar() != EOF)
while ((c = getchar()) != EOF)
but there has been a problem that the content in the first printf() haven't appeared until I entered some words.
stdout
通常是缓冲的。它可能是 line 缓冲或 完全缓冲 或根本没有。使用 fflush(stdout)
确保发布输出。
printf("Please enter character:\n");
fflush(stdout); //add
while (c = getchar() != EOF) {
printf("%d\t", c);
fflush(stdout); //add
}
printf("%d - at EOF\n", c);
#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 选项,因为当该选项可用时,句子 'Please enter character:' 位于正确的位置。但是如果我不这样做,我就不能使用 Ctrl+D 来发送 EOF。此外,似乎只有当我在字符后的新空行中键入 Ctrl+D 时结果才能正确。
平台:Windows10,工具链:MinGW
顺便说一句,我也尝试过 Cygwin。同样的问题又出现了。有什么想法吗?
问题是
c = getchar() != EOF
真的只是
c = (getchar() != EOF)
你想要的是
(c = getchar()) != EOF
如果您使用 c = getchar() != EOF
$ cc -c test.c -Wall -Wextra test.c:5:1: warning: return type defaults to ‘int’ [-Wimplicit-int] main() { ^~~~ test.c: In function ‘main’: test.c:9:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses] while (c = getchar() != EOF) { ^
这就是建议启用警告的原因。对于新项目,我认为 -Wall -Wextra
是最低限度。
运算符优先级。 @Johnny Mopp
c = getchar() != EOF
和c = (getchar() != EOF)
一样,当然不是OP想要的。
// while (c = getchar() != EOF)
while ((c = getchar()) != EOF)
but there has been a problem that the content in the first printf() haven't appeared until I entered some words.
stdout
通常是缓冲的。它可能是 line 缓冲或 完全缓冲 或根本没有。使用 fflush(stdout)
确保发布输出。
printf("Please enter character:\n");
fflush(stdout); //add
while (c = getchar() != EOF) {
printf("%d\t", c);
fflush(stdout); //add
}
printf("%d - at EOF\n", c);