Why is the output stream only buffered when I use the sleep () function?

Given the following code

#include <stdio.h>

int main()
{
    while(1)
    {
        sleep(1);
        printf("X");
    }

    return 0;
}

Exit nothing happens until the buffer is full and subsequently automatically flushed by the system.

Why is it not buffered in this situation ?:

#include <stdio.h>

int main()
{
    while(1)
    {
        printf("X");
    }

    return 0;
}

The sleep () function seems to have some kind of hidden effect.

I am new to the concept of buffers, so any additional information or notes on my potential misconceptions are welcome.

+4
source share
1 answer

The output is still buffered, but buffer overflows (and thus flushing) occur so often in the second example that it cannot be seen.

, .

+4

Source: https://habr.com/ru/post/1536337/


All Articles