C语言编程—清空键盘缓冲区,Linux下完美实现

/*
*清空键盘缓冲区很多种方法,如用fflush(stdin);rewind(stdin);等,但是在linux这些都不起作用,
*发现setbuf(stdin, NULL);就能直接清空键盘缓冲区了。
*/





#include <stdio.h>
//清空缓冲区
int main()
{
	char ch1;
	char ch2;


	scanf("%c", &ch1);
	printf("ch1 = %d", ch1);
	setbuf(stdin, NULL);
    //fflush(stdin);//not work
	//rewind(stdin);//not work
	/*清空缓冲区,!!!NULL必须大写!!!*/
	scanf("%c", &ch2);
	printf("ch2 = %d", ch2);
	return 0;
}


posted @ 2015-10-17 12:39  cloudren2020  阅读(253)  评论(0编辑  收藏  举报