cprintfbufferflush

Why does printf flush or not flush depending on where the line is in a loop?


Why, in this program, does the printf function print or not print depending on where it is inside the loop?

#include <stdio.h>
#include <time.h>

int main()
{
    
    char frames[] = {'-', '\\', '|', '/'};
    
    int i = 0;
    time_t prev_time = time(0);
    while(1) {
        printf("\r%c", frames[i % 4]); //<- when placed here, it will flush
        if (time(0) - prev_time < 1) continue;
        //printf("\r%c", frames[i % 4]); <- when placed here, it needs to be manually flushed
        //fflush(stdout);
        prev_time = time(0);
        i++;
    }

    return 0;
}

Solution

  • It is not a behaviour of printf specifically, but rather of the stdout output stream. The stream is buffered and will emit output when one of the following occur:

    Note that what constitutes an interactive device is implementation-defined so there is scope for other behaviour perhaps.

    In the first instance, printf will be called many times very quickly and will rapidly fill the buffer (so will flush) in buffer sized blocks.

    In the second instance, the output is buffered slowly. It will eventually be output, but the buffer will take significantly more time to fill.

    If you were to step the first instance line by line in your debugger, you will notice that the output for that is also not immediate.

    The size of the buffer is implementation dependent, but for a 4096 byte buffer, it would take 34 minutes 8 seconds to fill and produce output.

    Placing a newline at the end of the output is the usual way to ensure immediate output without a flush.

    printf("%c\n", frames[i % 4]); 
    

    Will output immediately anywhere.