Is it Safe to Use fflush(stdin) in C Programming?

  • Thread starter Thread starter !kx!
  • Start date Start date
  • Tags Tags
    Input Stream
AI Thread Summary
The discussion centers on the use of fflush(stdin) in C programming, highlighting that its behavior is undefined according to the C standard. While some compilers, like MSVC, may clear the input buffer when fflush(stdin) is called, others, such as GCC, may do nothing or return an error. This inconsistency raises concerns about relying on fflush(stdin) for input stream management, as it can lead to unpredictable results across different systems and compilers. The conversation also touches on the general concept of streams in C, explaining that stdin is primarily an input stream and using it as an output stream is also undefined behavior. The importance of avoiding undefined behavior in programming is emphasized, as it can lead to various unintended outcomes.
!kx!
Messages
58
Reaction score
0
hi..
i need to ask something related to c programming.. (and i don't know if this is the right place to put up this question.. so please redirect me if necessary!)

i've read that using fflush(stdin) in C (to flush the input stream) is not a defined way to use fflush..
but, i continue to see it being used successfully in various instances.. please tell how and why it works..?
 
Technology news on Phys.org
fflush(stdin) has the effect of removing any data sitting in stdin's buffer at the moment of the call.
 
if it does that without any problem.. then why it is not advisable to use fflush() on input streams..??
 
hamster143 said:
fflush(stdin) has the effect of removing any data sitting in stdin's buffer at the moment of the call.
It might have that effect -- on your computer with your compiler. What effect it will have elsewhere, who knows?

From the C standard (emphasis mine),
int fflush(FILE *ostream);

ostream 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.


Intentionally invoking http://en.wikipedia.org/wiki/Undefined_behavior" is a very, very bad idea.
 
Last edited by a moderator:
ok..

and what happens to the data that is removed from the stream.. is it simply deleted.. or something else?
 
Who knows? What fflush does when applied to an input stream is undefined. What that means is that the compiler developers are free to do anything they please in response. fflush(stdin) might
  • Throw away any characters that happen to be in the input buffer,
  • Do absolutely nothing,
  • Cause your program to crash, or even
  • Make your computer emit http://catb.org/jargon/html/N/nasal-demons.html" .
 
Last edited by a moderator:
no i meant.. what will fflush() do to the data in output stream, if applied to it..
 
and what are 'nasal demons' ?!
 
!kx! said:
no i meant.. what will fflush() do to the data in output stream, if applied to it..
Different question! fflush() when applied to an output stream causes any buffered output to be sent to the output device. IO devices are very, very slow compared to memory. A program that uses unbuffered output often runs much slower than a similar program that uses buffered output. In unbuffered output, characters are immediately sent to the output device. In buffered output, the output handler stores output data and later sends the buffered data to the output device en masse. Typically this happens when the output buffer becomes full or when some special character such as a newline is sent. You as a programmer have some control over how the buffering works via the function setvbuf().
 
Last edited:
  • #10
!kx! said:
and what are 'nasal demons' ?!
Click on that term. It is a hyperlink; you can see that by the underlining and the fact that your cursor changes from a normal arrow to a hand with a finger when you move your mouse over the text.
 
  • #11
yeah.. sorry.. didn't see the hyperlink!
and thnx for all that help..
 
  • #12
D H said:
Who knows? What fflush does when applied to an input stream is undefined. What that means is that the compiler developers are free to do anything they please in response.

Strictly speaking, that's up to standard library developers rather than compiler developers. MSVC standard library and any others compatible with it (such as Intel library for Windows) do what I said: wiping all pending data from the input buffer. GCC standard library (glibc) does nothing and returns an error.

As long as we're nitpicking, I might as well point out that stdin is not necessarily a read-only stream.
 
  • #13
[As long as we're nitpicking, I might as well point out that stdin is not necessarily a read-only stream.[/QUOTE]

can u please shed some light on what a stream is in general... and what are different types of 'em..?
 
  • #14
hamster143 said:
Strictly speaking, that's up to standard library developers rather than compiler developers.
Strictly speaking, yes. Practically speaking, no. C compilers almost always come bundled with a library.

MSVC standard library and any others compatible with it (such as Intel library for Windows) do what I said: wiping all pending data from the input buffer. GCC standard library (glibc) does nothing and returns an error.
That's because the response to fflush(stdin) is undefined. What that means is that implementors of fflush are free to do whatever they wish when passed an input stream (or an update stream on which the last operation was input) and remain compliant with the standard. Programmers are strongly recommended to avoid undefined behavior.

As long as we're nitpicking, I might as well point out that stdin is not necessarily a read-only stream.
O rly?

stdin is the standard input stream. Using it as an output stream is once again undefined.
 
  • #15
stdin is the standard input stream. Using it as an output stream is once again undefined.

It is initially opened as an input stream. There's nothing to keep you from calling freopen("filename", "wb", stdin), or, on many platforms, even doing "stdin = stdout; "!
 
Back
Top