Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is fflush in C
3.
Syntax
4.
Parameters
5.
Return type of fflush in C?
6.
Exceptions of fflush in C
7.
Example
7.1.
C
7.2.
C
8.
Frequently Asked Questions
8.1.
What does Fflush in C do?
8.2.
Explain the undefined behavior of fflush in C with the standard input stream.
8.3.
How to flush a file pointer in C?
9.
Conclusion 
Last Updated: Mar 27, 2024
Easy

fflush in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The fflush in C is an important tool for managing input and output streams. In this article, we will discuss the fflush function in detail. We will understand the syntax, parameters, exceptions, and a few examples of the implementation of fflush in C. 

fflush in C

In the C programming language, a buffer is a temporary storage area in the RAM that holds the data being read or written to an input/output stream. Simply put, when we write data to a file or output stream, it is first stored in the buffer before it is written to a file. The buffering helps optimize the write operation by reducing the number of calls to the operating system.

What is fflush in C

In C programming language, fflush is a function used to flush a stream's output buffer. fflush sends any unwritten data to the stream and clears the buffer. It is used to make the output visible to the user immediately by printing the data of the buffer to the specific file present in the main memory. This is why the fflush function is commonly used in real-time applications and interactive programs.

The fflush function can be used with any output stream like stdout, stderr, etc. You cannot use the fflush function without including the <stdio.h> library in your C program.

The fflush function ensures data consistency, improves the C program's performance, and is quite helpful in handling errors. 

Also read - Input Buffering in Compiler Design

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

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);
}

Output:

Hello Ninja!

 

The C program below uses the fflush function with the standard output and the standard error stream.

Code:

  • C

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;
}

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: 

  1. Data types in C
     
  2. Strings in C
     
  3. Ternary Operator in C
     
  4. Conditional Operator in C
     
  5. Bubble Sort Program in C
     
  6. 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 ProgrammingJavaScriptSystem 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.

Previous article
difftime() function in C++
Next article
asctime() Function in C++
Live masterclass