2020.3.4 fflush(stdin)或者getchar吃掉垃圾字符
时间:2020-03-05 00:40:06
收藏:0
阅读:104
fflush(stdin)吃掉垃圾字符
# include <stdlib.h> int main(void) { int a, c; char b; printf("input a"); scanf("%d", &a); fflush(stdin);//将前一个scanf输入的缓冲区全部清空 scanf("%c%d", &b, &c); printf("a = %d, b = %c, c = %d\n", a, b, c); system("pause"); return 0; }
getchar吃掉垃圾字符
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 int a, c; 6 char b; 7 printf("input a: "); 8 scanf("%d", &a); //将前一个scanf输入的缓冲区通过循环全部清空 9 while (getchar() != ‘\n‘)//表示只要字符不是回车就继续吃缓冲区字符 10 { 11 ;//空语句 12 } 13 printf("\ninput b,c\n"); 14 scanf("%c,%d", &b, &c); 15 16 printf("a = %d, b = %c, c = %d\n", a, b, c); system("pause"); 17 }
原文:https://www.cnblogs.com/jiangzenghui/p/12416875.html
评论(0)