c语言中的一个奇怪问题? getchar()还未执行就消失了?

时间:2022-08-23 21:14:38
编写一个输入十个整数判断其中的最大值。可我输入最后一个整数后,本应执行
printf("%d is the largest number of the ten integels",largest);
可是屏幕就消失了,而我在其后添一个scanf语句,就能顺利执行。
以下是原程序及添加的一个scanf语句(蓝色字体):
#include<stdio.h>
int main()
{
int counter=1,number,largest;
printf("input a  integel\n");
scanf("%d",&largest);
 for(counter=1 ;counter<10;counter++){
printf("input another integel\n");
scanf("%d",&number);
 if(number>largest)
 largest=number;
   }
 printf("%d is the largest number of the ten integels",largest);
 [color=#00CCFF]scanf("%d",&largest);
 getchar();
 return 0;
}[/color]

10 个解决方案

#1


因为你输入数字的时候,输入缓冲区里面还有个'\n',然后getchar()就读入这个了,于是就结束了
所以要在读数以后fflush(stdin);
把这个回车吃掉.

#2


执行scanf("%d",&number); 这个后缓存里面还保留着你的回车字符,getchar()会直接读取,相当于你又输入一个字符。在这句后面加上fflush(stdin)把缓存清空一下就行。。

#3


引用 1 楼 baihacker 的回复:
因为你输入数字的时候,输入缓冲区里面还有个'\n',然后getchar()就读入这个了,于是就结束了
所以要在读数以后fflush(stdin);
把这个回车吃掉.
一语中的!UP

#4


不是getchar()还未执行,实际上printf 和getchar()两句都执行了,只不过没有让窗口暂停,所以你没有看到结果。你所添加的getchar()实际上已经吸收了上一句scanf输入后的回车符,所以它实际上已经执行完了,所以也就退出程序了。如果你在VC中调试的话,调试器会自动加一个“Press any key to continue”等待用户按任意键退出,以便用户看到结果。像TurboC等很多编译器都没有提供暂停功能。而且如果直接运行可执行程序也不会暂停,必须由程序添加暂停代码,实际上你所添加的scanf语句就是达到这个效果。
另外,实际上你可以在最后加上两个getchar()就可以了,一个用来吸收最后的会车符,一个用来暂停,不需要再写一个scanf语句。或者只写一个scanf语句,最后的那一个getchar()实际上没有太大作用。

#5



#include <stdio.h> 
int main() 

int counter=1,number,largest; 
printf("input a  integel\n"); 
scanf("%d",&largest); 
for(counter=1 ;counter <10;counter++){ 
printf("input another integel\n"); 
scanf("%d",&number); 
getchar(); /*这样也可以*/
if(number>largest) 
largest=number; 
  } 
printf("%d is the largest number of the ten integels",largest); 
 getchar();
return 0; 
}

#6



int fflush(FILE *stream);

如果 stream 指向输出流或者更新流(update stream),并且这个更新流
最近执行的操作不是输入,那么 fflush 函数将把这个流中任何待写数据传送至
宿主环境(host environment)写入文件。否则,它的行为是未定义的。

原文如下:

int fflush(FILE *stream);

If stream points to an output stream or an update stream in which
the most recent operation was not input, the fflush function causes
any unwritten data for that stream to be delivered to the host environment
to be written to the file; otherwise, the behavior is undefined.

建议使用下面的语句来清除输入缓冲中的'\n'
while ( getchar() != '\n' ) ;

#7



fflush(FILE *stream)是针对输出流的,如果 stream 指向输入流(如 stdin),那么 fflush 函数的行为是不确定的

#8


回车吃掉

#9


引用 7 楼 yangch_nhcmo 的回复:
C/C++ code
fflush(FILE *stream)是针对输出流的,如果 stream 指向输入流(如 stdin),那么 fflush 函数的行为是不确定的

都在说fflush清缓存,请教一下,这个缓存是放在什么位置?

#10


谢谢众位高手啊!
csdn还真是个卧虎藏龙的地方啊!

#1


因为你输入数字的时候,输入缓冲区里面还有个'\n',然后getchar()就读入这个了,于是就结束了
所以要在读数以后fflush(stdin);
把这个回车吃掉.

#2


执行scanf("%d",&number); 这个后缓存里面还保留着你的回车字符,getchar()会直接读取,相当于你又输入一个字符。在这句后面加上fflush(stdin)把缓存清空一下就行。。

#3


引用 1 楼 baihacker 的回复:
因为你输入数字的时候,输入缓冲区里面还有个'\n',然后getchar()就读入这个了,于是就结束了
所以要在读数以后fflush(stdin);
把这个回车吃掉.
一语中的!UP

#4


不是getchar()还未执行,实际上printf 和getchar()两句都执行了,只不过没有让窗口暂停,所以你没有看到结果。你所添加的getchar()实际上已经吸收了上一句scanf输入后的回车符,所以它实际上已经执行完了,所以也就退出程序了。如果你在VC中调试的话,调试器会自动加一个“Press any key to continue”等待用户按任意键退出,以便用户看到结果。像TurboC等很多编译器都没有提供暂停功能。而且如果直接运行可执行程序也不会暂停,必须由程序添加暂停代码,实际上你所添加的scanf语句就是达到这个效果。
另外,实际上你可以在最后加上两个getchar()就可以了,一个用来吸收最后的会车符,一个用来暂停,不需要再写一个scanf语句。或者只写一个scanf语句,最后的那一个getchar()实际上没有太大作用。

#5



#include <stdio.h> 
int main() 

int counter=1,number,largest; 
printf("input a  integel\n"); 
scanf("%d",&largest); 
for(counter=1 ;counter <10;counter++){ 
printf("input another integel\n"); 
scanf("%d",&number); 
getchar(); /*这样也可以*/
if(number>largest) 
largest=number; 
  } 
printf("%d is the largest number of the ten integels",largest); 
 getchar();
return 0; 
}

#6



int fflush(FILE *stream);

如果 stream 指向输出流或者更新流(update stream),并且这个更新流
最近执行的操作不是输入,那么 fflush 函数将把这个流中任何待写数据传送至
宿主环境(host environment)写入文件。否则,它的行为是未定义的。

原文如下:

int fflush(FILE *stream);

If stream points to an output stream or an update stream in which
the most recent operation was not input, the fflush function causes
any unwritten data for that stream to be delivered to the host environment
to be written to the file; otherwise, the behavior is undefined.

建议使用下面的语句来清除输入缓冲中的'\n'
while ( getchar() != '\n' ) ;

#7



fflush(FILE *stream)是针对输出流的,如果 stream 指向输入流(如 stdin),那么 fflush 函数的行为是不确定的

#8


回车吃掉

#9


引用 7 楼 yangch_nhcmo 的回复:
C/C++ code
fflush(FILE *stream)是针对输出流的,如果 stream 指向输入流(如 stdin),那么 fflush 函数的行为是不确定的

都在说fflush清缓存,请教一下,这个缓存是放在什么位置?

#10


谢谢众位高手啊!
csdn还真是个卧虎藏龙的地方啊!