0 c
好吧,我只是在学习C并偶然发现这个练习代码来计算角色K&R的书:
#include <stdio.h>
/* count characters in input; 2nd version */
main()
{
double nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}
Run Code Online (Sandbox Code Playgroud)
问题是我不知道当我输入任何字符时是否打印字符数量是因为没有任何输出,只有空格(getchar()等待另一个输入).
有人可以向我解释发生了什么事吗?我在openSUSE 11.3中使用vim练习bash.
您必须将EOF信号/字符发送到程序.如果从终端窗口内运行它,请按Ctrl+ D.
如果你是从文件管道,如下:
./my_program < input_file_name
Run Code Online (Sandbox Code Playgroud)
然后它将自动工作.