Using fflush() in C

Filed Under: C Programming
Fflush C

In this article, we’ll take a look at how we can use the fflush() function in C.

The purpose of this function may not be immediately clear to you, but you might take a hint from the name of the function.


The fflush() function in C

The fflush() function (Flush to File) is a function which flushes the contents of any output stream (or update stream) to the corresponding file.

fflush(file);

Why do we need this function, and not just something like fprintf() or printf()?

The answer is due to how the Operating System handles this input. Whenever we pass any output, the Operating System actually puts the content (which will be eventually written to the output file – like stdout) in a buffer.

This buffer will automatically get flushed at a file only at certain instances.


Understanding the need for fflush()

To illustrate a common issue that we face due to output buffering, consider the following code snippet.

#include <stdio.h>

int main() {
    fprintf(stdout, "This is to stdout. ");
    fprintf(stderr, "This is to stderr. ");
    fprintf(stdout, "This is also to stdout. ");
}

You might expect that the output comes in order, but the reality is a bit different.

This is to stderr. This is to stdout. This is also to stdout. 

Why does this happen? This is because of how the files for stdout and stderr are buffered.

By default, stdout is newline-buffered, while stderr is unbuffered.

What does this mean? For stdout, until there is a newline, the contents of the output stream are actually stored inside a temporary buffer.

Only when the output stream encounters a newline (or end of file), will the contents be written to the stdout file. Notice the lack of a newline in our program!

But, for stderr, the stderr stream is not buffered, so the contents are immediately written to it!

So, in our example program, since we don’t have a newline, stderr will be written first. Only after the end of file (output) is reached, will the contents of stdout be written!

So if you want to have stdout output first, without using a newline, you must flush the output buffer using fflush(). Makes sense?

Let’s now add fflush() after writing to stdout, so that the contents are instantly written to the output file.

#include <stdio.h>

int main() {
    fprintf(stdout, "This is to stdout. ");
    // Flush the contents of the output stream immediately
    fflush(stdout);
    fprintf(stderr, "This is to stderr. ");
    // No need to flush stderr since it is un-buffered
    fprintf(stdout, "This is also to stdout. ");
    // Flush the contents of the output stream immediately
    fflush(stdout);
}

Output

This is to stdout. This is to stderr. This is also to stdout. 

Indeed, we have our problem solved for us, using fflush()!


Can I use fflush(stdin) for Input Streams?

The short answer is: No, don’t ever use this function.

The logic of flushing buffered output stream to files makes sense, since we know that the output contents will be eventually be written to a file / stream

But for input streams, there is no way of knowing where the input will end up eventually. For all you know, there may be some external program which feeds input randomly to this program, and you have no way of knowing what will happen to it!

So, code of the form: fflush(stdin) is undefined behavior!!!


Conclusion

In this article, we learned how we could use the fflush() function in C, to flush output buffers to files.

For similar articles, do go through our tutorial section on C programming!


References


Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages