【发布时间】:2021-10-12 07:23:58
【问题描述】:
我正在阅读 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;
}
【问题讨论】:
-
由于编译警告,这不是现代标准的好代码,但它是正确的。现在应该写成
while (c = (getchar() != EOF)) printf("%d\n", c);,它将打印1,直到遇到EOF。 -
没有编译错误。
-
这也是一个糟糕的代码,因为它把赋值填塞到
while()表达式中。将赋值填塞到条件表达式中非常容易出错,它是 MISRA 等处理安全等关键问题的标准中被禁止的样式。 -
这本书是你尝试的第一个吗?由于整本书都使用了隐式 int 规则,因此从那里开始的每个程序都会在现代编译器上引发至少一个警告。