我正在阅读 Clovis L. Tondo 和 Scott E. Gimpel 的 C 答案书,以了解他们如何编写解决此问题的解决方案。
这是它在那本书中的显示方式:
#include <stdio.h>
main()
{
int c;
while (c = getchar() != EOF) /* <-- This test results in compilation errors */
printf("%d\n", c);
printf("%d - at EOF\n", c);
}
将上述代码保存到文件中时编译错误,ex1.6.c如下所示:
bash-3.2$ clang -Wall ex1.6.c
ex1.6.c:2:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^
ex1.6.c:6:12: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
while (c = getchar() != EOF)
~~^~~~~~~~~~~~~~~~~~
ex1.6.c:6:12: note: place parentheses around the assignment to silence this warning
while (c = getchar() != EOF)
^
( )
ex1.6.c:6:12: note: use '==' to turn this assignment into an equality comparison
while (c = getchar() != EOF)
^
==
2 warnings generated.
因此,看起来 C 答案书中的解决方案是错误的。我对吗?
这是我尝试的解决方案:
#include <stdio.h>
int main() {
int c;
printf( "%d\n",( getchar() != EOF));
return 0;
}