我是C的初学者,我购买了“C语言程序设计”一书,并开始阅读并完成它所说的一切。书中有一个代码应该列出句子的行数,字数和字符数。这是本书的代码。程序不打印数字
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
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);
}
出于某种原因,应该用printf打印出的值没有显示出来。我真的不知道什么是错的。如果将printf放在while循环中,它只会打印出数字,但这不可能是正确的,因为每次值更改时都会打印数字。
它看起来对我很好。如果将它放入调试器并在printf上放置一个断点并等待它达到,会发生什么情况。是否有可能printf正在执行,然后命令窗口在你真正看到输出之前关闭?尝试在printf之后添加一个getchar()。 –
请注意,您已写入c ='\ t'而不是c =='\ t'。 –
这肯定是本书的一个错字。我也在printf后面放了一个getchar(),但没有任何反应。 – captain