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

Laurel Fan laurel at sdf.lonestar.org
Wed Aug 7 12:26:34 EST 2002

On Wed, Aug 07, 2002 at 11:52:15AM -0400, Malcolm-Rannirl wrote:
> On Wednesday 07 August 2002 02:31 am, Anand R wrote:
> > 	Just a small doubt. I have seen many flushing stderr in their code.
> > Why is it necessary to flush stderr?? A small code example below:
> One I can actually answer. :)
> When you write to stderr (or any other file), the output is buffered by the 
> OS, because it's not very efficient to write everything to disk immediately. 
> Unfortunately, if your program (or the OS, as I've had happen with Windows) 
> crashes then those buffers die with it and anything in them is lost. 

Actually, I believe every output stream/file _except_ for stderr is
buffered by default (in C using stdio).  Therefore stderr is the only
stream that you _don't_ need to flush, which is one reason why stderr
is a good place to put errors.  If you see fflush(stderr), it's
probably from habit (and it won't do any harm).  (Of course, you can
make stderr buffered, or make anything else unbuffered, see manpage
for setbuf).

laurel at sdf.lonestar.org

More information about the Courses mailing list