When is FILE flushed? When is FILE flushed? windows windows

When is FILE flushed?


If the library implementation can determine the output stream not to refer to an interactive device (and only then), the stream will be fully buffered, i.e. it will be flushed when the buffer (by default of BUFSIZ size) is full.

If not fully buffered, a stream can be line buffered, i.e. it will be flushed when an '\n' is written (or the buffer is full, if your line is really long), or unbuffered.

(ISO/IEC 9899:1999, chapter 7.19.5.3 "The fopen() function", paragraph 7. Don't have a newer version of the standard at hand, but AFAIK this didn't change.)

What constitutes an "interactive device" is implementation-defined. (Chapter 5.1.2.3 "Program execution", paragraph 6.)

The general idea is that file output should be fully buffered whereas terminal output be line buffered (or unbuffered, as Jesse Good correctly pointed out).

Both the buffering policy and the buffer size can be changed via setvbuf(). Note that any such change must happen before you start accessing the stream, which is somewhat obvious once you think about it.