fflush() Function

The fflush() function in C is used to flush a stream’s output buffer, ensuring that any buffered data is written to the underlying file. It is commonly used when you need to make sure that output is immediately visible or saved, and it can also flush all open output streams when called with a null pointer.


Syntax of fflush()

</>
Copy
int fflush(FILE *stream);

Parameters

ParameterDescription
streamA pointer to a FILE object that identifies the stream to be flushed. If stream is a null pointer, all output streams are flushed.

Note that if the stream was opened for reading, flushing its input buffer is not portable and depends on the specific library implementation. The stream remains open after calling fflush().

Return Value

The function returns 0 on success. If an error occurs, it returns EOF and sets the error indicator on the stream (see ferror()).


Examples for fflush()

Example 1: Flushing the Output Buffer to Ensure Immediate Display

This example demonstrates how to flush the standard output buffer so that the output is immediately displayed on the console before waiting for user input.

Program

</>
Copy
#include <stdio.h>

int main() {
    printf("Enter your name: ");
    fflush(stdout);  // Flush output to ensure the prompt is displayed immediately

    char name[50];
    scanf("%49s", name);
    printf("Hello, %s!\n", name);

    return 0;
}

Explanation:

  1. The program prints a prompt asking the user to enter their name.
  2. The fflush(stdout) call forces the output buffer to be flushed, ensuring the prompt is visible immediately.
  3. The user inputs their name, which is then read using scanf().
  4. The program greets the user by printing their name.

Program Output:

Enter your name: John
Hello, John!

Example 2: Flushing All Output Streams Using NULL

This example shows how to flush all open output streams by passing a null pointer to fflush().

Program

</>
Copy
#include <stdio.h>

int main() {
    // Writing to stdout and stderr
    fprintf(stdout, "This is a standard output message.\n");
    fprintf(stderr, "This is an error message.\n");

    // Flush all output streams
    fflush(NULL);

    return 0;
}

Explanation:

  1. The program writes messages to both stdout and stderr.
  2. The fflush(NULL) call flushes all open output streams, ensuring all buffered data is written to their respective outputs.
  3. This is particularly useful in debugging or when immediate output is required from multiple streams.

Program Output:

This is a standard output message.
This is an error message.

Example 3: Flushing a File Stream After Writing

This example demonstrates the use of fflush() to flush a file stream after writing data to a file, ensuring that all data is saved immediately.

Program

</>
Copy
#include <stdio.h>

int main() {
    FILE *fp = fopen("example.txt", "w");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }

    fprintf(fp, "Data written to file.\n");
    // Flush the file stream to ensure data is written immediately
    if (fflush(fp) != 0) {
        perror("Error flushing file stream");
    }

    fclose(fp);
    return 0;
}

Explanation:

  1. The program opens a file example.txt for writing.
  2. A string is written to the file using fprintf().
  3. The fflush(fp) call forces the output buffer of the file stream to be flushed, ensuring that the data is immediately written to the file.
  4. The file is then closed using fclose().

Program Output:

Data written to file.