帮我看看这个C程序怎么没输出

来源:百度知道 编辑:UC知道 时间:2024/09/22 11:29:01
程序功能:统计标准输入的数字、空白符、其他字符的数量并在标准输出打印统计结果

#include <stdio.h>
/* count digits, white space, others */
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);
}

这个程序来自《The+C+Programming+language》
不应该有错。
在rhel AS 4u7上编译通过,但没输出结果,初步判断是while循环出不来
这么经典的书不应该是错的,至少作者调试通过才会编入本书的。
没错。我已经把while下面的几行{}了。

我用的GCC,如何改才能有输出呢?改EOF?

很显然,while循环无法退出。EOF 是文件结尾的标志,不同的编译器定义不太一样。输入时没有检测到EOF,所以while一直循环。并且,while下面的几行代码应该用{}括起来。

EOF 是文件结尾的标志,在windows下是ctrl+z,在linux下是ctrl+d
首先在ctrl+z或者+d之前 必须无数据,或者必须先有个回车,ctrl + z后要按回车。

这个程序我的机器上能运行并结果 (vc++上编译的)
你开始执行后入完数据后按下2次ctrl+z,完了回车就能运行出结果
文件的结尾标记对应着ctrl+z