Why is fflush()
not considered safe? I have been told that it shows undefined behaviour? If so, what is the alternative to fflush()?
2 Answers
You were probably told fflush(stdin)
is undefined and that's right. But fflush
is the way to go for other streams that you want to flush, like stdout
for example.
To clarify, the stream must be used for output. The standard says:
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.
Later the standard includes this explicitly as "Unspecified behavior":
The stream for the fflush function points to an input stream or to an update stream in which the most recent operation was input

- 178,505
- 25
- 365
- 392
-
Drive-by downvoter: I am worried for you. Come now and comment, we may still be able to help you. – cnicutar Feb 24 '14 at 20:54
You've misunderstood.
Calling fflush()
on an input stream has undefined behavior as far as the C standard is concerned Some systems and/or secondary standards such as POSIX may define its behavior, but it's not a good idea to depend on that in code that's intended to be portable.
Calling fflush()
on an output stream, for example fflush(stdout)
, is perfectly safe and well defined. It causes any pending output to be delivered to the operating system (which doesn't guarantee that it will be delivered to the physical output device).
In some cases where fflush
is called on an input stream, you should actually read and discard input characters until, for example, you see a newline '\n'
or EOF
. Or, often better, use fgets
or something similar to read an entire line at a time, and then parse the input.

- 254,901
- 44
- 429
- 631
-
+1 It *can* be called on an in-out stream so long as the last-op was an `out`, not an `in` (as i recall anyway. I further recall that intervening transitions from `out` to `in` ops *require* either a `fflush()` *or* a seek-op. Apologies in advance if i remember incorrectly, as I rarely find myself manipulating files in full ii-mode. – WhozCraig Feb 24 '14 at 19:46
-
but my college professor taught me that it is ok and iam very much confused as it works well with input streams also – OldSchool Feb 24 '14 at 19:46
-
1@Bayant_singh If your college professor taught you `fflush()`ing an input-only stream is all well and good, you're not the only one that should be seeking education. It may "work" on your platform, but don't count on it "work"ing as a language standard-based rule. – WhozCraig Feb 24 '14 at 19:47
-
1@Bayant_singh: The behavior of `fflush(stdin)` is not defined by the C language standard. It is defined by the secondary POSIX standard; see the link in my answer. But even if you want to rely on the POSIX definition, most cases where `fflush(stdin)` is used are better handled by reading and discarding characters. – Keith Thompson Feb 24 '14 at 19:48
-
-
-
@KeithThompson: why it isn't guaranteed that pending output will be delivered to the physical output device? What is the reason? – Destructor Mar 18 '16 at 09:46
-
@Destructor: Because a C program (that calls `fflush`) can't necessarily control the underlying behavior of the operating system. – Keith Thompson Mar 18 '16 at 15:57