Indeed, if a stream is line buffered and all output to it ends with a newline, then for the purposes of ensuring that output is not delayed and that you do not lose output on abnormal termination, being line buffered is just as good as being unbuffered. (A similar statement can be made if manual use of fflush
is done consistently where needed.)
However, the statement you made and that you're relying on, that "stdout
is line buffered (by default)", is false. Rather, except for stderr
, all stdio files are line-buffered (or possibly unbuffered) by default only when connected to an interactive device (tty). Otherwise they are fully buffered.
Per 7.21.3 Files, ¶7 (emphasis mine):
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.
and 7.21.5.3 The fopen
function, ¶8:
When opened, a stream is fully buffered if and only if it can be determined not to refer to an interactive device. The error and end-of-file indicators for the stream are cleared.