This documentation is archived and is not being maintained.


Flushes a stream.

int fflush( 
   FILE *stream 


Pointer to FILE structure.

Return Value

fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error.

Note   If fflush returns EOF, data may have been lost due to a write failure. When setting up a critical error handler, it is safest to turn buffering off with the setvbuf function or to use low-level I/O routines such as _open, _close, and _write instead of the stream I/O functions.


The fflush function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. fflush negates the effect of any prior call to ungetc against stream. Also, fflush(NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream.

Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream. The commit-to-disk feature of the run-time library lets you ensure that critical data is written directly to disk rather than to the operating-system buffers. Without rewriting an existing program, you can enable this feature by linking the program's object files with COMMODE.OBJ. In the resulting executable file, calls to _flushall write the contents of all buffers to disk. Only _flushall and fflush are affected by COMMODE.OBJ.

For information about controlling the commit-to-disk feature, see Stream I/O, fopen, and _fdopen.


Function Required header Compatibility
fflush <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_fflush.c
#include <stdio.h>
#include <conio.h>

int main( void )
   int integer;
   char string[81];

   /* Read each word as a string. */
   printf( "Enter a sentence of four words with scanf: " );
   for( integer = 0; integer < 4; integer++ )
      scanf( "%s", string );      
      // Security caution!
      // Beware allowing user to enter data directly into a buffer
      // without checking for buffer overrun possiblity.
      printf( "%s\n", string );

   /* You must flush the input buffer before using gets. */
   fflush( stdin );   // fflush on input stream is an extension to the C standard
   printf( "Enter the same sentence with gets: " );
   gets( string );
   printf( "%s\n", string );


This is a test
This is a test

Sample Output

Enter a sentence of four words with scanf: This is a test
Enter the same sentence with gets: This is a test
This is a test

See Also

Stream I/O Routines | fclose | _flushall | setvbuf | Run-Time Routines and .NET Framework Equivalents