调用格式
int fflush ( FILE * stream );
返回:
- 当成功时,返回0
- 发生error时,返回EOF,并设置错误提示符
参数:
- 指向一个指定缓冲流( buffered stream)FILE实体的指针
- 如果stream为null指针,则all such streams are flushed.
简单实例
#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
printf("%d\n", c);
scanf("%c", &c);
printf("%d\n", c);
return 0;
}
运行这个程序,输入1, 并按enter键,结果为:
49//是字符1对应的ASCII值
10//enter键对应的ASCII值,第二个scanf函数执行了,并从缓冲区中得到了值
利用fflush函数将缓冲区中不理想的值冲掉
...
scanf("%c", &c);
printf("%d\n", c);
fflush(stdin); // 冲掉缓冲区中的无用值
scanf("%c", &c);
printf("%d\n", c);
...