4 c
现在我正在读一本关于C的书,并且在书中遇到了一个我无法工作的例子.
#include <stdio.h>
#define IN 1
#define OUT 0
main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
Run Code Online (Sandbox Code Playgroud)
它应该计算输入中的行数,单词数和字符数.但是,当我在终端中运行它似乎什么都不做.我错过了什么或者这个代码有问题吗?
程序仅在输入结束时终止(getchar返回EOF).当在终端上运行时,这通常不会发生,因此似乎程序被卡住了.您需要在Linux上按Ctrl + D(可能两次)手动关闭输入,或者在Windows的行开头按F6和Enter(不同的系统可能使用不同的方法).