The POSIX and C standards explicitly state that using fflush
on an input stream is undefined behavior. The fflush
is defined only for output streams.
#include <stdio.h>
int main()
{
int i;
char input[4096];
scanf("%i", &i);
fflush(stdin); // <-- undefined behavior
gets(input);
return 0;
}
There is no standard way to discard unread characters from an input stream. On the other hand, some implementations uses fflush
to clear stdin
buffer. Microsoft defines the behavior of fflush
on an input stream: If the stream is open for input, fflush
clears the contents of the buffer. According to POSIX.1-2008, the behavior of fflush
is undefined unless the input file is seekable.
See Using fflush(stdin)
for many more details.