Fflush in C

The function fflush is used for flushing the stream’s output buffer; that is, it is used to immediately flush the contents present in any of the buffered stream to the corresponding file. It will return zero if successful, else it returns EOF, and the error indicator will be a set, such as feof.

Syntax:

where;

            Stream – pointer to a FILE object that specifies a buffered stream.

Stream points to an output stream or an update stream in which operation was not an input. Fflush causes any unwritten data of that stream to be delivered to the other host environment to be written into the file; else will cause undefined behavior.

Fflush will write the contents of the stdout buffer immediately to the output file instead of a newline buffered writing by default. This should not be used on any of the input streams as it is prone to undefined behavior. Fflush(stream) is defined as output streams; hence, fflush(stdout) will work fine, but fflush(stdin) does not.

Consider an instance that includes fflush(stdin):

Output:

Consider an instance for fflush(stdout):

In the above code, the compiler runs the program and will keep on buffering into the output ‘s’ until and unless it faces calls to fflush(), later it starts buffering the output again and sleeps for 3 seconds. It then sends the remaining output to the STDOUT before the program comes out.

Output:

Need for fflush() function:

Consider an example:

A programmer would expect that the output comes in the same order in which the input will be given. But, the actual output will be different.

Instead of getting the output as: Points to stdout. Points to stderr. Points to stdout;

The actual output will be: Points to stderr. Points to stdout. Points to stdout.

This anomaly is due to how the file for stdout and stderr are buffered. By default, stdout is newline-buffered while stderr is unbuffered. In other words, for stdout, until there is a newline character or end of a line (EOF) is defined, the contents of the output stream are stored inside a temporary buffer.

When the output stream encounters a newline or end of the line, the contents will be written only to the stdout file. But in stderr, the streams are not buffered, so the contents are immediately written into it. Since in the above program, a newline character is missing out, stderr will be written first, and only after the end of file is reached, the contents of stdout will be written. If you need stdout in the first, you must flush the output buffer using the fflush function without using a newline or end of the line.

Revised program example;

Output:

Usage of the fflush function in stdin gives out undefined behavior. The logic behind this is that the flushing buffered output stream to the files is acceptable since we know that the output will be written to a file or a stream. Using fflush for input streams makes it difficult for the compiler to take in, as there is no way of knowing where the input will end up. However, some of the compilers, such as Microsoft visual studio, allow fflush(stdin). It is used in these compilers by taking an input string with spaces; the buffer does not get cleared for the next inputs and considers the previous input.

Pin It on Pinterest

Share This