fflush() in C
Overview
Buffer is a temporary memory (mainly part of RAM or may be defined by the user in the program in form of a character array) to store the output either given by the program or a particular function and the buffer memory present in RAM, prints the output to the main destination when it gets full means no memory left to add more data, end line character (\n) hits, or when the output is provided by the unbuffered function.
fflush() function in C is used to flush the buffer of any stream present in RAM, which means it prints the data of the buffer to the respective file present in the main memory. fflush() in C is defined in the <stdio.h>. fflush() function in C takes only a single parameter which is a pointer to the file object, for example, stdout.
Syntax of fflush() in C
Syntax of flush() in C is simple as it takes only a single parameter.
Parameters of fflush() in C
The fflush() function in C takes only a single parameter which is a pointer to the File Object in which we want to flush or write our data. The best example of File Object is stdout, stderr, etc.
Return Value of fflush() in C
The return type of the fflush() function in C is zero if the function call is successful which means the buffer is flushed and if it is unsuccessful then it will return an error that is End of File or eof in form of a positive integer and an indicator is set to indicate feof. To learn about the errors like eof, feof, etc please check the article Error Handling in C.
Example
In this example, we are going to use the flush() function of the C programming language to print the data of buffered memory on the console without using the end-line character (\n). As the printf() function of C is a buffered function means data given as output by this function is first to go to the buffer rather than going to the console or the main file. So we are going to use the fflush() in C to forcefully do it. Code:
Input: 5
Output:
Explanation: In the above code, we have used the fflush() function of C to clear the buffer but this doesn’t show any difference compared to if we don’t use flash in C here. Consider this example as just a basic code to show the syntax use of flush() in C, we will later in this article discuss and even see the code output where the fflush() function will make a change in the output.
Exceptions of fflush() in C
fflush function in C doesn’t throw any exception usually but fflush in C may show undefined behavior in some cases like it may or may not work fine with the stdin file. We will discuss the stdin case in detail later in this article.
What is fflush() in C?
Before going to the fflush() in C in detail let’s see some terms which are related to the fflush() in the C function. Normally, printing any data or simply any output from the function gets stored in any provided file which is stored in the main memory or secondary memory, and every time writing data in the file is costly in terms of time or other resources for the operating system. To overcome this issue operating system creates a temporary memory known as a buffer in the RAM or the primary memory to store the data and print it later because primary memory is easily and fastly accessible as compared to the main memory where the output file is present. But sometimes this may become a reason for printing the value earlier or later as compared to the expected position or time. Data in buffer memory getflushedwhen any new-line character (‘\n’) hits, buffer memory becomes full, or any unbuffered function is called. Now, buffered output functions are those functions whose output is stored in the buffer memory, and the unbuffered output functions are those functions whose output didn’t score in the buffer and it flushes the buffer as well.
Now let’s see an example where using the buffered function before the unbuffered function provides an output other than the expected one. Code:
Output1:
Output2:
Output3:
Note: All the outputs are printed for the same code with the different types of compilers. In the above code, first thing is that we have three different outputs, yes it is possible and depends upon the compiler.
First, we used the print()` function of C to print the data into different steams which are stdout and stderr. Here stdout is a buffered stream that keeps its data in buffered memory while stderr is the unbuffered stream it prints directly to the console or the storage file. In the first and second outputs, we can see statement1 and statement3 are combined printed while statement2 is printed either before or after them. In the third case, we didn’t get the output of statement2.
Output1:
Here output for the stdout stream is stored in different memory or place while for stderr is in another and to show them on the console compiler prints first for stdout and after then.
Output2:
Here compiler didn’t print the output for the stderr stream and only showed for the stdout stream.
Output3:
As stdout is buffered steam or more specifically new-line buffered while stderr is unbuffered steam so when we are going to print statement2 it will be printed before statement1 and then statement3 will be printed.
Now let’s use the fflush() in the C function and get out the expected output, which is, as statement1 is printed before statement2 and similarly statement2 is printed before the statment3 in our code, in output also they must be present in the same order: Code:
Output:
In the above code, we flushed out the buffer using the fflush() in the C function and got our expected output.
Note: For the previous code, the compilers which are providing the second output are going to show this change, and for other compilers which were showing the output1 and output3 the output will remain the same.
More Examples
Example 1: Using fflush() Function With Input Stream
We can use fflush(stdin) but it is not recommended to do so because while we are flushing the stdout steam we know that the data from there is going to be printed on the file or stream but the data from the stdin stream will be lost, how? Let's take an example: In this code, we are declaring an array and taking user input for the size of the array, and all the elements of the array, and we will try to print the elements of the array. Here we will take input using a file and add all the data that is size and elements of the array at a single time in the file. In the code, we will use the fflush(stdin) method to flush the input stream after taking input only of the size of the array. This means information on elements of the array is lost and our array will contain the garbage value. Code:
Input:
Output:
Note: Garbage value may change from compiler to compiler.
Explanation: In the above code, we used the "freopen()" file method to take the user input through the input.txt file and give all the input at the same time. Further, we have taken the input of the array size and flushed the input stream which means all the data of the “input.txt” file is erased and all the array elements will contain the garbage value. Also, if we comment the fflush(stdin) then this code will work fine.
Also, if we flush the output file it will be flushed to another location but if we will flush the stdin then it will not make sense it will be like, we are providing some input and we are flushing it.
Conclusion
- Buffer is a `temporary memory to store the output and later it prints the output to the main destination.
- Buffer gets empty when it gets full, end line character (\n) hits, or when the output is provided by the unbuffered function.
- fflush() function in C is used to flush the buffer of any stream and it prints the data of the buffer to the respective file. fflush() in C is defined in the <stdio.h>.
- fflush() function in C takes only a single parameter which is a pointer to the File Object in which we want to flush or write our data.
- The return type of the fflush() function in C is zero if the function call is successful and if it is unsuccessful then it will return an error that is End of File or of and an indicator is set to indicate feof.
- fflush() function in C can be used with the stdin stream but it is not recommended.