对于输入输出缓冲区的清空操作
对于这样一段简单的代码:
#include<stdio.h>
int main(void)
{
int a;
scanf("%d",&a);
char c=getchar();
printf("a=%d c=%c",a,c);
}
当执行输入为:
123gfh
时,打印的结果为
123 g
当对代码稍作修改之后:
#include<stdio.h>
int main(void)
{
int a;
scanf("%d",&a);
fflush(stdin);//新添加
char c=getchar();
printf("a=%d c=%c",a,c);
}
再次执行上面的输入就会发现,在输入123gfh之后程序不会停止,fflush会将缓冲区清空,缓冲区清空之后不会再收到gfh字符,所以需要再次输入新的字符
参考链接:[1].https://blog.csdn.net/qq_34793133/article/details/85713413