【发布时间】:2011-09-10 22:15:18
【问题描述】:
在这个非常棒的网站上搜索有关 getchar() 函数的所有内容时,我发现了这篇文章:Why doesn't getchar() wait for me to press enter after scanf()?
#include <stdio.h>
int main()
{
int value;
printf("1. option 1.\n2. option 2.\n3. option 3.\n4. Exit\n\nMake an option: ");
scanf("%d", &value);
switch (value)
{
case 1:
printf("you selected the option 1.");
break;
case 2:
printf("you selected the option 2.");
break;
case 3:
printf("you selected the option 3.");
break;
case 4:
printf("goodbye");
break;
default:
printf("thats not an option");
break;
}
getchar();//here is the question,why it's useful ?
return 0;
}
我了解整个程序,并且我了解每次调用它时,getchar 都会从文本流中读取下一个输入字符并将其作为其值返回。也就是之后
c = getchar();
变量 c 包含输入的下一个字符。字符通常来自 键盘。
但是问题来了:为什么程序员在程序结束时调用getchar()?
【问题讨论】: