Syntax
The syntax for fflush in C programming language is:
int fflush(FILE *stream);
Parameters
The fflush function takes only one argument as its parameter, which acts as a pointer to a file object of a specific stream (like stdout or stderr) being flushed.
Return type of fflush in C?
The fflush function returns a zero value on a successful buffer flush. A non-zero value with an End Of File error indicates unsuccessful operation completion.
Exceptions of fflush in C
The fflush function is primarily used for flushing a stream's output buffer. That is, it is not used with input streams like stdin. Thus the function results in undefined behavior when used with a stdin file. This is because when a fflush function is called on an input stream, only a part of the input buffer is flushed, whereas the rest remains unprocessed.
Hence, to avoid this unpredictable exception of the C program, avoiding fflush with the input stream is recommended.
Must Read Decision Making in C
Example
The C program below uses the fflush function to immediately print the buffer data to the console and write it to the stdout stream.
Code:
C
#include <stdio.h>
int main()
{
printf(“Hello Ninja!");
// here, fflush stdout makes sure that the output is printed before the program ends.
fflush(stdout);
}

You can also try this code with Online C Compiler
Run Code
Output:
Hello Ninja!
The C program below uses the fflush function with the standard output and the standard error stream.
Code:
C
#include <stdio.h>
int main() {
// This will be buffered
printf("Hello, Ninja!\n");
// Flush stdout buffer to display the message
fflush(stdout);
// This will be buffered
fprintf(stderr, "This is an error message\n");
// Flush stderr buffer to display the message
fflush(stderr);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
Hello, Ninja!
This is an error message.
In the above example, printf and fprintf statements write to stdout and stderr, respectively. To ensure that the messages are displayed immediately in the console, fflush is used to clear the buffer and display the messages.
Must Read Passing Arrays to Function in C
Frequently Asked Questions
What does Fflush in C do?
In C, the fflush() function is used to flush or clear the internal buffer associated with a file stream. It forces any buffered data to be written to the associated file or, in the case of an input stream, clears any unread data from the buffer.
Explain the undefined behavior of fflush in C with the standard input stream.
The fflush function results in undefined behavior when used with a stdin file. This is because when a fflush function is called on an input stream, only a part of the input buffer is flushed, whereas the rest remains unprocessed. This gives rise to an unpredictable situation in the C program.
How to flush a file pointer in C?
To flush a file pointer in C, we can use the fflush() function. The fflush() function in C is used to immediately write buffered data to a file, ensuring data is saved promptly.
Conclusion
Kudos, Ninja, on making it to the finish line of this article! We have studied that fflush is a very important function in C that is used to manage the output streams such that the data is visible to the user immediately. We also saw the implementation of the fflush function with the standard output and error streams.
We hope this blog has helped you understand the concept of fflush in C better. Keep learning! We recommend you read some of our other articles on C:
-
Data types in C
-
Strings in C
-
Ternary Operator in C
-
Conditional Operator in C
-
Bubble Sort Program in C
-
C++ Interview Questions
Do check out The Interview guide for Product Based Companies as well as some of the Popular interview problems from top tech companies like Amazon, Adobe, Google, Uber, Microsoft, etc.
Refer to our guided paths on Coding Ninjas Studio to learn more about Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.