Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C fflush() function

Reference for C fflush(): force buffered output to be written, make prompts appear, share a file between two handles, and why fflush(stdin) is not portable.

Streams collect output in a buffer and write it to the file or terminal in larger chunks. fflush() forces that write to happen now: whatever the stream has buffered is handed to the operating system.

You need it in a few specific situations: when a prompt without a newline must appear before the program waits for input, when another program or another handle must see the data immediately, and before operations that switch a stream from writing to reading.

C
#include <stdio.h>

int fflush(FILE *stream);

Try it

C Buffered data is invisible until flushed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Prompts and fflush

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int fflush(FILE *stream);
Name Description
stream An output stream (or an update stream whose last operation was output) to flush. If NULL, all such streams are flushed.

Returns: 0 on success, EOF on a write error (with errno set and the stream's error flag raised).

Errors / undefined behavior: flushing an input-only stream is undefined behavior in the C standard. In particular, fflush(stdin) is not a portable way to throw away pending input. glibc happens to define it for seekable files, and Microsoft's library clears the buffer, but other systems do something else.

Usage notes

  • A newline usually flushes a terminal. Standard output is line buffered when it goes to a terminal, so printf("done\n") appears at once. When output goes to a file or pipe it is fully buffered, and nothing appears until the buffer is full, the program ends, or you flush.
  • Prompts. Output without a newline needs fflush(stdout) to be sure it appears before the program blocks on input. Many terminals flush stdout when you read from stdin, but that is not guaranteed.
  • Flushing does not mean durable. fflush() hands data to the operating system. It may still sit in the system's cache. To make sure it reaches the disk, call fsync(fileno(f)) on POSIX after fflush().
  • Crashes lose buffered output. If a program is killed or crashes, anything still in a buffer is lost. Logs written for debugging are often flushed after every line, or written to unbuffered stderr.
  • Switching direction. On an update (+) stream, call fflush() (or a positioning function) between writing and reading.
  • Mixing handles. Two FILE objects for the same file have independent buffers. Flush the writer before the reader looks, as in the first example.
  • Performance. Flushing after every small write defeats buffering. Flush when the reader needs the data, not by habit.
  • Clearing unwanted input is done by reading: int c; while ((c = getchar()) != '\n' && c != EOF) { }. See getchar().
  • stderr is unbuffered by default, so errors appear immediately and never need flushing. You can change buffering with setvbuf().