Using fflush on an input stream

suggest change

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

    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.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents