C language input and output buffer

buffer buffers

The immediate echoing of input characters is an instance of unbuffered (or direct ) input, meaning that the characters you type are immediately made available to the waiting program.

The delayed echoing, on the other hand, illustrates buffered input, in which the characters you type are collected and stored in an area of temporary storage called a buffer. Pressing Enter causes the block of characters you typed to be made available to your program.

Why have buffers? First, it is less time-consuming to transmit several characters as a block than to send them one by one. Second, if you mistype, you can use your keyboard correction features to fix your mistake. When you finally press Enter, you can transmit the corrected version.

Unbuffered input, on the other hand, is desirable for some interactive programs. In a game, for instance, you would like each command to take place as soon as you press a key. Therefore, both buffered and unbuffered input have their uses.

Buffering comes in two varieties: fully buffered I/O and line-buffered I/O. For fully buffered input, the buffer is flushed (the contents are sent to their destination) when it is full. This kind of buffering usually occurs with file input. The buffer size depends on the system, but 512 bytes and 4096 bytes are common values. With line-buffered I/O, the buffer is flushed whenever a newline character shows up. Keyboard input is normally line buffered, so that pressing Enter flushes the buffer.

ANSI C and subsequent C standards specify that input should be buffered, but K&R originally left the choice open to the compiler writer.

Echoed input means the character you type shows onscreen, and unechoed input means the keystrokes don’t show.

There is no standard ANSI way of invoking unbuffered input; the means depend on the computer system.

Guess you like

Origin blog.csdn.net/chengkai730/article/details/132198469