It's because puts
is also outputting a newline character which, on devices that can be determined to be interactive, causes flushing by default (for standard output) (a).
You should see a similar effect if your initial printf
outputs a newline at the end:
printf("hello, world\n");
or if you fflush (stdout);
before the sleep()
call.
The relevant part of C11
is 7.21.3 Files
, section /7
:
At program startup, three text streams are predefined and need not be opened explicitly
— standard input (for reading conventional input), standard output (for writing
conventional output), and standard error (for writing diagnostic output). As initially
opened, the standard error stream is not fully buffered; the standard input and standard
output streams are fully buffered if and only if the stream can be determined not to refer
to an interactive device.
This harks back to the days of C89/90 4.9.3 Files
:
At program startup, three text streams are predefined and need not be opened explicitly --- standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). When opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.
(a): It's not quite that simple. For starters, this is implementation dependent simply because the standard states that what constitutes an interactive device is implementation dependent (the behaviour is specified but not the thing that affects that behaviour).
Secondly (as per here), the standard only mandates when standard output becomes fully bufferd (when the device is definitely not interactive). Whether it's unbuffered or line buffered for interactive devices is an implementation decision.