fflush in C
In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout).
The fflush() function is used to clear the output buffer and move the buffered data to the console or disk. It is basically used for output stream only.
The fflush(flush to file) function flushes the content of any output stream to the corresponding file.
Syntax of fflush
fflush(FILE *ostream)
Here, ostream will point to the update or output stream in which the recent operation is not an input operation. The function that causes any non-written data (for that stream) to be delivered to the host environment should be written in the file; otherwise, it acts as undefined.
Need of fflush()
The problems faced in output buffering is shown below -
Code
#include <stdio.h>
int main() {
fprintf(stdout, "This is to stdout. "); // statement 1 prints to stdout
fprintf(stderr, "This is to stderr. "); // statement 2
fprintf(stdout, "This is also to stdout.");// statement 3
}
The output expected was in order, but it comes out a bit different as -
This is to stderr. This is to stdout. This is also to stdout.
It happens because, by default, stdout is a new line-buffered, and stderr is unbuffered. In clear terms, for stdout until there is a new line, the content will be stored in the same temporary buffer. This is the reason that there is no newline.
For stderr, it is unbuffered, so the content is immediately written there.
Hence until we reach the end of the file(output end), the content of the stdout will be written in new lines.
Now let us add fflush() after every stdout.
Code
#include <stdio.h>
int main() {
fprintf(stdout, "This is to stdout. "); // statement 1
// Flush the contents of the output stream immediately
fflush(stdout);
fprintf(stderr, "This is to stderr. "); // statement 2
// No need to flush stderr since it is un-buffered
fprintf(stdout, "This is also to stdout. "); // statement 3
fflush(stdout); // adding stdout after statement 3
}
This time the output generated is -
This is to stdout. This is to stderr. This is also to stdout.
Here we have added fflush after statement 1 and after statement 3, and we get the output as desired. There was no need to add fflush for stderr as it is unbuffered.
fflush(stdin)
We cannot use fflush for stdin because buffering to the output screen makes logical sense as we know it will eventually be printed on the screen. And for inputs, there is no way to know when the inputs will end eventually. Thus, fflush(stdin) gives undefined behavior.
For taking inputs that contain spaces between them, we can use fflush(stdin).
Code
#include <stdio.h>
#include<stdlib.h>
int main()
{
char str[20]; // create char array
int i;
for (i=0; i<2; i++) // take two inputs
{
scanf("%[^\n]s", str); // take input
printf("%s\n", str); // print input
fflush(stdin); // clearing the buffer to take second input
}
return 0;
}
Input
Java
Javatpoint
Output
Java
Javatpoint
Note: We can use fflush() after the scanf() function as it also clears the buffer, but due to the undefined behavior, it is not recommended to use fflush(stdin).