我正在关注“The C Programming Language. 2nd Edition”,并且已经达到“1.5.2 Character Counting”。
为使用空语句的字符计数器提供的代码如下:
#include <stdio.h>
main() {
double nc;
for(nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}
然而程序不输出输入的字符数:
input
input
而如果我包含大括号并忽略空语句:
#include <stdio.h>
main() {
double nc;
for (nc = 0; getchar() != EOF; ++nc) {
printf("%.0f\n", nc);
}
}
...它提供了正确的输出:
input
0
1
2
3
4
5
input
6
7
8
9
10
11
如何使程序的空语句版本工作?