Does NEWLINE always flush stdio buffer?

Chris Torek chris at mimsy.UUCP
Sun Jul 2 06:08:56 AEST 1989


In article <11012 at ihlpl.ATT.COM> knudsen at ihlpl.ATT.COM (Knudsen) writes:
>I'd always understood that printf'ing any string ending in '\n'
>(newline) would flush the I/O buffer.

Nope.

>However, I've found that this will not always flush if stdout has been
>redirected to a pipe.

Yep.  Nor (on 4BSD Unix) a socket (there, a pipe *is* a socket), nor
(on any Unix, and any pANS system) any ordinary file.

>Does anyone know the "official" rules of the traditional stdio
>library?

The `official' rule is that `interactive' output (like that going to
a terminal or a window) is supposed to be line buffered by default,
and that any other output is supposed to be fully buffered by default.
An application can decide for itself by using setbuf or setvbuf.

I feel the best approach is for applications to set all output files
to `fully buffered', and then to use fflush whenever the output is
supposed to appear.  This never produces surprises, and it also tends
to run faster.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list