C语言,while(scanf("%d",&item[i])!=EOF)的用法,如何跳出循环,具体的!

如题所述

输入数据结束想退出循环时 拍入 组合键 Ctrl-Z 就是 EOF.
int item[100];
int i,n;
i=0;
while(scanf("%d",&item[i])!=EOF) { i++;}
n=i;
printf("I have read:\n");
for (i=0;i<n;i++) printf("%d ",item[i]);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-12-18
EOF在C语言宏定义中是-1也就是当输入的不等于-1的时候继续循环。
这样的输入方式,是acm吗?追问

s

追答

推荐这一个ppt给你
杭电的初学者课件,里面有关于ACM输入输出的说明。很全的

本回答被提问者采纳
第2个回答  2013-12-18
The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.

我的理解,不对请指正:
输入时,我们将字符从键盘输入到标准输入缓存stdin文件中,然后利用函数从这个文件中读取出来,再赋值给其他变量,这些函数有 read gets getchar scanf。。。等等,都包含在stdio.h头文件中,当函数读取stdin文件失败时就会返回EOF,或者读到文件结束(End Of File 缩写EOF)也会返回EOF(宏定义 -1 值)。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网