在这个非常棒的网站中搜索有关 getchar() 函数的所有内容时,我发现了这篇文章:为什么 getchar() 不等我在 scanf() 之后按 Enter 键?
#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()?