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

  • Thread starter Thread starter !kx!
  • Start date Start date
  • Tags Tags
    Input Stream
Click For Summary

Discussion Overview

The discussion revolves around the use of fflush(stdin) in C programming, specifically addressing its defined behavior, implications, and the potential consequences of its usage. Participants explore the technical aspects of input and output streams, the undefined behavior associated with fflush on input streams, and the differences in implementation across various compilers and standard libraries.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants note that fflush(stdin) is not defined by the C standard for input streams, leading to undefined behavior.
  • Others mention that on certain compilers, fflush(stdin) may remove data from the input buffer, but this behavior is not guaranteed across different environments.
  • There is a question about what happens to data removed from the input stream when fflush is called, with some participants expressing uncertainty about the outcome.
  • Participants discuss the behavior of fflush when applied to output streams, indicating that it flushes buffered output to the output device.
  • Some participants clarify that the behavior of fflush(stdin) can vary between different standard libraries, with specific examples from MSVC and GCC.
  • There is a mention of stdin not being strictly a read-only stream, and the implications of using it as an output stream are also noted as undefined.

Areas of Agreement / Disagreement

Participants express differing views on the safety and implications of using fflush(stdin). There is no consensus on its appropriateness, and multiple competing perspectives on its behavior and effects remain throughout the discussion.

Contextual Notes

The discussion highlights the undefined nature of fflush(stdin) and the variability in behavior across different compilers and standard libraries. Participants also point out the potential risks of invoking undefined behavior in programming.

!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; "!
 

Similar threads

Replies
18
Views
10K
  • · Replies 20 ·
Replies
20
Views
28K
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K