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:
- a
\n (newline) appears in the stream (for an interactive device),
- the buffer becomes full,
- the stream is explicitly flushed.
- When input from
stdin is requested and stdin and stdout refer to the same interactive device.
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.
stdoutauto flushes when it sees\nor when the buffer is full (which could be 4096). Because you do\r, you should dofflushperiodically. For a "progress message", at least once per second. The simplest is to always do it after yourprintfbut this can slow execution.printfbefore theifand uncomment theprintf/fflushafter it.printf("\r%c", frames[i % 4]);==>printf( "\r%c", "-\\|/"[ i & 3 ] );(no need forframes[]), and use anunsignedcounter that can legally overflow without triggering UB...