我开始学习C,我的一个朋友(比我大)推荐了Brian Kernighan和Dennis Ritchie编写的C语言.
然而,在尝试一些例子时,他们的行为与预期不同,而不是书中所写.
例如,这个,似乎不起作用(什么都不打印):
#include
#define IN 1
#define OUT 0
/* count lines, words, and characters in input */
int 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);
return 0;
}
你认为我应该继续读这本书,也许只有一些错误,值得一读吗?或者有一本更好的C语言初学者书.
编辑:我的假设是用ENTER键发送输入,将模拟EOF,但不是. CTRL Z的确如此.
谢谢你的帮忙.