[Courses] Re: [C] A clarification on fflush(stderr)

Akkana akkana at shallowsky.com
Thu Aug 8 13:57:10 EST 2002


Laura Bowser writes:
> stderr is *supposed* to be unbuffered according to the ANSI C standard - this
> doesn't mean that it is..  In *most* systems, stderr can be flushed by
> putting a newline at the end of a printf format string, but if you want to
> *guarantee* that you get your output at that time, use fflush.

The reason a newline flushes the output buffer on stdout and stderr
(at least on most systems) is that those those streams are line
buffered.  Stdio (standard I/O) treams can be fully buffered (many
characters are saved up and written as a block), line buffered (written
as soon as a newline is seen), or unbuffered (each character will be
written immediately).  By default, a stream that goes to a terminal
(such as stdout and stderr if you don't redirect them) is line buffered,
any other stream is fully buffered; but you can change the buffering for
any stream using the setbuf command, or you can use fflush to flush a
stream.

This is documented in man setbuf (but it took some digging around in
stdio.h to remind myself which man page to look in).

	...Akkana



More information about the Courses mailing list