gcc c语言中scanf输入格式不正确,清空缓冲区问题

我的博客:www.while0.com

我的博客:www.shishangguan.net

折磨了一下午,只因为fflush(stdin)再gcc里和vc里表现不一致。gcc里不能够清空缓冲区。直接上例子:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int a = 0;
 7     /**
 8      **如果输入的是字母而不是整数,则要先清空缓冲区,不然循环时会直接从缓冲区拿走刚才输入不通过的字母,变成死循环
 9      **/
10     while(a == 0){
11         printf("a请输入一个整数:
");
12         //fflush(stdin);                        //vc平台可以清空缓冲区,gcc不可以。
13         while ((getchar()) != '
');            //把缓冲区里的数据读干净,实现清空缓冲区的效果
14         scanf("%d", &a);
15     }
16     printf("%d", a);
17     return 0;
18 }
原文地址:https://www.cnblogs.com/yamadie/p/3237211.html