top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why do we use fflush(stdin) in programming specially C and C++?

+3 votes
901 views
Why do we use fflush(stdin) in programming specially C and C++?
posted Dec 11, 2015 by Mishthy Mukherjee

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You would use fflush(stdout) to ensure that whatever you just wrote in a file/the console is indeed written out on disk/the console.

The reason is that actually writing, whether to disk, to the terminal, or pretty much anywhere else, is pretty slow. Further, writing 1 byte takes roughly the same time as writing, say, a few hundred bytes[1]. Because of this, data you write to a stream is actually stored in a buffer which is flushed when it is full or when you call fflush. Calling fflush means you are accepting that your function call will take a bit of time but that you are 100% sure that you want this out right away.

As for fflush(stdin), the reason people call it is that some functions, scanf in particular, sometimes leave the input in a counter-intuitive state.

For instance, say you call scanf("%d", &n) and then fgets(buf, 100, stdin), expecting an input of the form:

10
line

Well it turns out that what you will get is an empty line on the fgets because scanf didn't move you past the '\n' after it read the 10.

Because of that kind of problem, people use fflush(stdin). This is because at the time you call it, the '\n' following "10" is in the buffer but not the string "line\n". THIS IS A TERRIBLE IDEA. IT ONLY WORKS ON WINDOWS.

In it is explicitly said in the standard that fflush should only apply to output streams. This is because things that just don't make sense would happen if you used it out input streams.

See, there is absolutely nothing that indicates that "line\n" will not be in the buffer at that time. Sure it works when writing input manually, but this might fail if the user is copying and pasting into your program, and is even less likely to work if someone redirects a file as input to your program. Heck, even just a fast typer, a slow computer and a bit of luck might mess this up...

Because of this it seems, the implementation on my Mac simply ignores fflush(stdin). I don't know what happens on other platforms but I'm sure there's at least one that has the behavior I described earlier where you might just lose some of your input because of an fflush. Point is, don't use it on input streams.

answer Dec 11, 2015 by Manikandan J
Similar Questions
+7 votes
#include<stdio.h>

int &fun()
{
   static int x;
   return x;
}   

int main()
{
   fun() = 10;
   printf(" %d ", fun());

   return 0;
}

It is fine with c++ compiler while giving error with c compiler.

+7 votes

Where we need to use volatile variable? If possible please provide some real time example ?

+1 vote

As far as I know, CORE file contains all the information till Segmentation fault. Then, while debugging core file why we are using executable file(binary) with it.?

...