Board logo

标题: 一个白痴的问题,别打我 [打印本页]

作者: lujiayi3     时间: 2005-10-27 16:22    标题: 一个白痴的问题,别打我
MAIN()
{
int c;

c=getchar();
while(c!=EOF)
{
putchar(C);
c=getchar();
}
}

请问为什么不是
MAIN()
{
int c;
c=getchar();
 putchar(C);
}

作者: jinql     时间: 2005-10-31 14:23    标题: 下面有且仅有一个。
下面有且仅有一个。

作者: GOTOmsdos     时间: 2005-10-31 16:27
按下面的代码运行,你就明白啦!
关键是: 输入字符,并按下回车后,才运行while.还有,第一次getchar()是等待用户的输入(与人交互的),而第二次getchar()是程序自动的从刚刚输入的字符中(已存入键盘缓冲区)读取的..

(字符中如果含有x,就会退出,否则循环)
#include <stdio.h>
main()
{
int c;
int count=0;
c=getchar();
while(c!='x') /* 如果是 EOF 就是死循环 */
{
putchar(c);
c=getchar();
count++;
printf("\nthis is the end of round %d\n",count);
}
}

Last edited by GOTOmsdos on 2005-10-31 at 16:29 ]

作者: LanE     时间: 2005-11-4 11:28
Originally posted by lujiayi3 at 2005-10-27 04:22 PM:
MAIN()
{
int c;

c=getchar();
while(c!=EOF)
{
putchar(C);
c=getchar();
}
}

请问为什么不是
MAIN()
{
int c;
c=getchar();
 putchar(C);
}


因为目的不同,下面的那个是接受一个字符,然后输出该字符,程序结束
上面的那个是直到遇到EOF才结束
不过,注意大小写<img src="images/smilies/face-laugh.png" align="absmiddle" border="0">

TO 3楼,EOF也勉强可以的,比如在使用管道的时候

BTW:这个这个例子好像是K&R的白皮书里面的例子..-_-UU