what is actually fflush()

what is the use of fflush() give any example program output using fflush() and not using fflush()

There are three standard i/o streams. The most common two among them being stdin and stdout.

When you print something using the printf or cout, the output is never written ‘to the screen’. But, the output is sent to stdout. (stdout acts like a buffer)

By default, the output sent to stdout is then sent to the screen (we can redirect this to other files/streams as we like). Similarly, stdin is by default mapped to the keyboard, but can be redirected to any other file/stream.

Now, stdout is, by default, line buffered. This means, the output sent to stdout is not sent to the screen for display (or to the redirected files/streams) until it gets a newline character in it. So, if you want to override the default buffering behaviour, then you can use fflush to clear the buffer (and in the process, send everything to the screen/file/stream).

Consider the two programs:


#include <stdio.h>

void delay(unsigned long long ctr)
{
    while (ctr > 0)
        --ctr;
}

int main()
{
    printf("Hello, ");
    delay(1000000000ULL);

    printf("world!\n");
    delay(1000000000ULL);

    printf("Welcome.\n");
    return 0;
}

#include <stdio.h>

void delay(unsigned long long ctr)
{
    while (ctr > 0)
        --ctr;
}

int main()
{
    printf("Hello, ");
    fflush(stdout);
    delay(1000000000ULL);

    printf("world!\n");
    delay(1000000000ULL);

    printf("Welcome.\n");
    return 0;
}

If you run the above two programs, both of them outputs

Hello, world!
Welcome.

But since a delay is added (you can adjust the value of ctr) in between all three print statements, both program will behave differently when run.

The first program will wait for some time, then print ‘Hello, world!’ at once, then wait again for some time, then print ‘Welcome.

The second program will print ‘Hello, ’ wait for sometime, then print ‘world!’, again wait for sometime and then print ‘Welcome.

I hope my explanation can give a brief idea about why fflush is used for.

6 Likes

The final output does not differ between a program with fflush and a program without it.Only, when the output actually appears differ.

For a program that does not involve much computation between to print statements, this can hardly be noticed. (That is why, I added a long delay between the print statements.)

When a process is finished, all of its streams are flushed irrespective of whether a newline is issued at the end or not.

my doubt is
when we use print(“hello,”);before fflush()the hello will be temporarily will be stored in buffer when we use fflush(stdout) the hello which is there in stdout will be deleted so in output how hello will be printed
if i use fflush before printf why same output will come.

actually my doubt is after hello goes into the buffer the buffer will be deleted or the buffer will be deleted before the hello goes into the buffer explain with example.

I did not completely understand your query. Can you give your sample code, for what you are talking about? It will help us better answer your question. :slight_smile:

//