代码如下;
#include <stdio.h>
int main()
{
int count = 0;
int ch;
while ((ch = getchar()) != EOF)
count++;
printf("All of the characters is %d.\n", count);
return 0;
}
程序目的是计算输入的字符个数,我分别输入了三种情况的数据,得到情况如下:
1、什么都不输入直接按【ctrl + D】 (Linux系统) 程序直接退出。
2、abc【ctrl + D】【ctrl + D】程序退出。注意:这个时候我需要按两下才能退出。
3、abc【回车】这个时候光标移动到了下一行行首,我再按【ctrl + D】程序退出。
ctrl + d 只有 当缓冲区为空 时才起作用。
第1种情况:没有输入,缓冲区为空。
第3种情况:有输入,但按下了回车,**回车的作用就是清空缓冲区。**
这两种情况下,按一次ctrl + d 就行。
第2种情况:有输入,但没有按回车,此时缓冲区不为空。第一次ctrl + d清空缓冲区,第二次ctrl + d 才发送EOF。
深入学习可以参考:
http://blog.csdn.net/sole_cc/article/details/40383033
如果对您有帮助,请采纳答案好吗,谢谢!