C语言清空输入缓冲区

出自转载

方法1

setbuf(stdin, NULL);

使用setbuf(stdin,null);可以使stdin输入流由默认缓冲区转为无缓冲区.

方法2

while ((c = getchar()) != EOF && c != '\n');

使用getchar()函数将stdin缓冲区中的字符全部取出, 等效为清空缓冲区.

方法3(误)

 fflush(stdin); 

fflush on input stream is an extension to the C standard(fflush 操作输入流是对 C 标准的扩充), 所以并非是C标准的一部分, 可移植性方面考量并不好用, 所以并不推荐学习使用。

posted @ 2018-08-29 13:19  jeffmeoi  阅读(370)  评论(0)    收藏  举报