Go to the previous, next section.

Buffering Concepts

There are three different kinds of buffering strategies:

Newly opened streams are normally fully buffered, with one exception: a stream connected to an interactive device such as a terminal is initially line buffered. See section Controlling Which Kind of Buffering, for information on how to select a different kind of buffering.

The use of line buffering for interactive devices implies that output messages ending in a newline will appear immediately--which is usually what you want. Output that doesn't end in a newline might or might not show up immediately, so if you want them to appear immediately, you should flush buffered output explicitly with fflush, as described in section Flushing Buffers.

Line buffering is a good default for terminal input as well, because most interactive programs read commands that are normally single lines. The program should be able to execute each line right away. A line buffered stream permits this, whereas a fully buffered stream would always read enough text to fill the buffer before allowing the program to read any of it. Line buffering also fits in with the usual input-editing facilities of most operating systems, which work within a line of input.

Some programs need an unbuffered terminal input stream. These include programs that read single-character commands (like Emacs) and programs that do their own input editing (such as those that use readline). In order to read a character at a time, it is not enough to turn off buffering in the input stream; you must also turn off input editing in the operating system. This requires changing the terminal mode (see section Terminal Modes). If you want to change the terminal modes, you have to do this separately--merely using an unbuffered stream does not change the modes.

Go to the previous, next section.