【发布时间】:2021-01-17 12:09:20
【问题描述】:
我正在关注“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
如何使程序的空语句版本工作?
【问题讨论】:
-
您需要输入
<Enter><Ctrl+D>(Un*x) 或<Enter><Ctrl+Z>(Windows) 以向程序发出输入结束信号 (EOF),否则它会“卡在”@ 987654328@.
标签: c null character charactercount