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

In summary, fflush(stdin) is not a reliable way to flush the input stream in C programming. Its behavior is undefined and can vary depending on the compiler and library being used. It is recommended to avoid using fflush() on input streams to prevent potential issues and undefined behavior. Additionally, stdin is initially opened as an input stream but can be used as an output stream with undefined results.
  • #1
!kx!
58
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
  • #2
fflush(stdin) has the effect of removing any data sitting in stdin's buffer at the moment of the call.
 
  • #3
if it does that without any problem.. then why it is not advisable to use fflush() on input streams..??
 
  • #4
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:
  • #5
ok..

and what happens to the data that is removed from the stream.. is it simply deleted.. or something else?
 
  • #6
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:
  • #7
no i meant.. what will fflush() do to the data in output stream, if applied to it..
 
  • #8
and what are 'nasal demons' ?!
 
  • #9
!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; "!
 

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

What is "flushing" the input stream in C?

Flushing the input stream in C refers to the process of clearing out any remaining data in the input buffer. This ensures that the next input operation will start fresh and not be affected by any leftover input.

Why is it important to flush the input stream in C?

Flushing the input stream is important because it helps to prevent any unexpected behavior in the program. If there is any leftover input in the buffer, it may interfere with the expected input and cause errors or bugs in the program.

How do you flush the input stream in C?

In C, the input stream can be flushed using the "fflush()" function. This function takes in the stream pointer as an argument and clears out any remaining data in the input buffer for that specific stream.

When should you flush the input stream in C?

It is recommended to flush the input stream after every input operation. This ensures that the buffer is always cleared and ready for the next input. However, if the program does not require user input after a certain point, it is not necessary to flush the input stream.

Can you flush the input stream multiple times in C?

Yes, the input stream can be flushed multiple times in C. However, it is important to note that flushing the input stream when there is no data in the buffer will not have any effect. It is only necessary to flush the input stream when there is leftover data in the buffer.

Similar threads

Back
Top