Rasteril
Rasteril

Reputation: 615

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

Considering the following code

#include <stdio.h>

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

    return 0;
}

The output is nothing, until the buffer gets overflowed and subsequently flushed automatically by the system.

Why does it then not get buffered in this situation?:

#include <stdio.h>

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

    return 0;
}

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

I'm new to the concept of buffers, therefore any additional information or notes about my potential misconceptions are welcomed.

Upvotes: 4

Views: 70

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409166

The output is still buffered, but the overflowing of the buffer (and thereby the flushing) happens so often in the second example that it's impossible to notice it.

If you run this on a system much slower than todays modern gigahertz PC's, you will definitely notice a difference.

Upvotes: 4

Related Questions