C语言 fflush(stdin)在c编程中有什么用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18170410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is the use of fflush(stdin) in c programming
提问by Mahi
I have the following program
我有以下程序
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ans[8];
int i;
for(i=1;i<=3;i++)
{
printf("\n What is the unit of traffic ?");
scanf("%s",ans);
fflush(stdin);
if(stricmp(ans,"Earlang")==0)
{
printf("\nAnswer is correct");
exit(1);
}
else
if(i<3)
printf("\n Try Again!\n");
}
printf("\n Nunit of traffic is Earlang:");
}
What is the use of fflush(stdin) in this program?
这个程序中的fflush(stdin)有什么用?
回答by Yu Hao
It's not in standard C, so the behavior is undefined.
它不在标准 C 中,因此行为未定义。
Some implementation uses it to clear stdinbuffer.
一些实现使用它来清除stdin缓冲区。
From C11 7.21.5.2 The fflush function, fflushworks only with output/update stream, not input stream.
来自 C11 7.21.5.2 fflush 函数,fflush仅适用于输出/更新流,不适用于输入流。
If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
如果 stream 指向一个输出流或一个更新流,其中最近的操作没有输入,则 fflush 函数会导致该流的任何未写入数据被传递到主机环境以写入文件;否则,行为未定义。
回答by iabdalkader
it clears stdinbuffer before reading. From the man page:
它stdin在读取之前清除缓冲区。从手册页:
For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.
对于输出流, fflush() 通过流的底层写入函数强制写入给定输出或更新流的所有用户空间缓冲数据。对于输入流, fflush() 会丢弃从底层文件中获取但尚未被应用程序使用的任何缓冲数据。
Note: This is Linux-specific, using fflush()on input streams is undefined by the standard, however, most implementations behave the same as in Linux.
注意:这是特定fflush()于Linux 的,标准未定义在输入流上使用,但是,大多数实现的行为与 Linux 中的相同。
回答by user2671945
It's an unportable way to remove all data from the input buffer till the next newline. I've seen it used in cases like that:
这是从输入缓冲区中删除所有数据直到下一个换行符的不可移植的方式。我已经看到它在这样的情况下使用:
char c;
char s[32];
puts("Type a char");
c=getchar();
fflush(stdin);
puts("Type a string");
fgets(s,32,stdin);
Without the fflush(), if you type a character, say "a", and the hit enter, the input buffer contains "a\n", the getchar()peeks the "a", but the "\n" remains in the buffer, so the next fgets()will find it and return an empty string without even waiting for user input.
没有fflush(),如果你输入一个字符,说“a”,然后点击回车,输入缓冲区包含“a\n”,getchar()偷看“a”,但“\n”保留在缓冲区中,所以下一个fgets()将找到它并返回一个空字符串,甚至无需等待用户输入。
However, note that this use of fflush()is unportable. I've tested right now on a Linux machine, and it does not work, for example.
但是,请注意,这种使用fflush()是不可移植的。例如,我已经在 Linux 机器上进行了测试,但它不起作用。

