Easy Tutorial
❮ C Function Tolower C Exercise Example88 ❯

C Library Function - fflush()

C Standard Library - <stdio.h>

Description

The C library function int fflush(FILE *stream) flushes the output buffer of the stream.

Declaration

Here is the declaration for the fflush() function.

int fflush(FILE *stream)

Parameters

Return Value

On success, the function returns zero. If an error occurs, it returns EOF and sets the error identifier (i.e., feof).

Example

The following example demonstrates the use of the fflush() function.

#include <stdio.h>
#include <string.h>

int main()
{
   char buff[1024];

   memset(buff, '\0', sizeof(buff));

   fprintf(stdout, "Enabling full buffering\n");
   setvbuf(stdout, buff, _IOFBF, 1024);

   fprintf(stdout, "This is tutorialpro.org\n");
   fprintf(stdout, "This output will be saved to buff\n");
   fflush(stdout);

   fprintf(stdout, "This will appear while programming\n");
   fprintf(stdout, "Finally, sleep for five seconds\n");

   sleep(5);

   return 0;
}

Let's compile and run the above program, which will produce the following result. Here, the program saves buffered output to buff until the first call to fflush(), then starts buffering output again, and finally sleeps for 5 seconds. It will send the remaining output to STDOUT before the program ends.

Enabling full buffering
This is tutorialpro.org
This output will be saved to buff
This will appear while programming
Finally, sleep for five seconds

C Standard Library - <stdio.h>

❮ C Function Tolower C Exercise Example88 ❯