【发布时间】:2014-07-31 20:39:49
【问题描述】:
当我运行以下代码并输入一个句子时,我没有得到任何输出。光标只是转到一个新行。
我直接从书上复制了这个,并仔细检查了它是否有错误(kernighan & ritchie 的第一版 C 编程语言)
#include <stdio.h>
int main()
{
int c,i,nwhite,nother;
int ndigit[10];
nwhite=nother=0;
for(i=0;i<10;++i)
ndigit[i] = 0;
while (( c=getchar()) != EOF)
if(c>= '0' && c<= '9')
++ndigit[c-'0'];
else if (c==' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for( i=0; i<10; ++i)
printf("%d",ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite,nother);
return 0;
}
【问题讨论】:
-
I copied this straight off the book and double checked it for mistakes我认为 Kernighan & Ritchie 可能缩进了... -
"输入一个句子我没有得到任何输出。"你用EOF结束你的句子吗? (取决于操作系统,但 Ctrl+D/Ctrl+Z?)
-
getchar 应该在获取每个字符后自动发送 EOF ......不是吗? :S
-
@user3689367,不,使用
CTRL+Z -
你不能在 IDE 或调试器下单步执行它吗?我从不只是转储一些代码并期望它能够工作。
标签: c windows codeblocks