The concept of a buffer
While the computer is running, the CPU is responsible for processing and processing the data, while the hardware device is responsible for transmitting, representing or storing the data, such as printing, input \ Output, etc. In this process, the processing speed of different equipment varies greatly, which results in a certain degree of efficiency problem. As in the printer output process, if the CPU directly to the printer output, because the output speed is slow, the CPU will need to wait for the previous result output to complete before continuing to output the next result. Similarly, during the input process, the CPU needs to wait until the device i\o the data it is reading. In this process, because CPU speed is faster than i\o device, CPU is in wait state most of the time, so that the CPU utilization is very low, resulting in the waste of expensive resources of the system.
In order to solve the problem of the speed mismatch between CPU and peripheral hardware, improve CPU utilization, improve the parallel efficiency of the system, and introduce the concept of buffer in the system.
A buffer is an area of memory that is allocated independently in physical memory for temporary storage of data during data movement. (Wiki Data Buffer)
After using buffer, the data can be transferred to the buffer and then processed by other devices. Taking the above procedure as an example, the CPU can store the results in a buffer, and then the printer reads the data from it, and in the process of printer output, the CPU can process other data without waiting. During the input process, the CPU does not wait for the input to complete, the I\O device stores the user input in the buffer, then requests the CPU resources, the CPU can read the data directly from the buffer to process, thus greatly improves the CPU utilization and the operation efficiency.
Buffering of Standard i\o
The standard i\o provides full buffer, row buffer, unbuffered three buffering methods according to different application requirements.
Full buffering: The i\o operation is performed only when the delimited buffer is filled or the data is read to the end (performing the system-provided read\write operation). Disk file reads and writes are generally used this way.
Row buffering: The i\o operation begins when the input and output process encounters a newline character ' \ n ' or when the allocation buffer is full. This buffering method is commonly used in the reading and writing operations of terminals such as stdio and stdout.
No buffering: When data is generated, it is immediately processed by the appropriate device. Generally speaking, stderr (standard error) uses this buffering method, which makes it possible to get a response immediately when there is an error message. It is important to note that the standard library does not cache and does not imply that the operating system or device drivers do not cache .
Note that the above-mentioned buffering method for Stdio/stdout is not a direct rule of death. The language specification of some languages will give a certain limit to the buffer implementation, but not specific, but many standard I/O is implemented in the above way. You can refer to about streams and buffers.
i\o function--getchar
GetChar () is a C library function for character input whose declaration of functions is contained in the header file stdio.h, function definition int getchar (void).
The function of GetChar () is to read one character in the standard input stdin . The getchar reads the data from the standard input, while the stdin records the user input in a row buffer, that is, the i\o operation starts only when the user types the ENTER key or into the end of the buffer, that is, a character is read. This allows the user to enter more than one character at a time, and the buffer may not be empty after reading. When GetChar () is called again, if the buffer is not empty, GetChar () will read the characters directly in the buffer instead of waiting for user input. It can be assumed that GetChar () waits for the completion of the row buffer, not the completion of the user input, and that, as long as the buffer is not empty, getchar () can read the characters without waiting for the user to enter .
/*codeblocks13.12*/#include<stdio.h>intMainvoid){ CharCH =' /'; while(ch! ='\ n') {printf ("Enter a character:"); CH=GetChar (); printf ("\ n"); Putchar (CH); printf ("\ n"); } return 0;}
The running results of the program are as follows:
It is apparent that the subsequent execution does not require user input, and getchar () reads the data in the buffer directly. and for the read operation of the character, the newline character ' \ n ' is also treated as a char , rather than as a mere end flag.
Character input waiting for user input
GetChar () can read characters directly from the buffer without waiting for user input, but this approach may also lead to potential errors. Here are two types of incoming characters waiting for the user to enter.
1. Use the Getche () and Getch () functions. All read a byte from the keyboard, where the latter does not echo the word to the screen. When you read a character with these two functions, you read one keyboard input and only one by calling the function. If you call Getche () and the keyboard strikes ' ABC ', only one character ' a ' will be read. Other characters are invalid input.
2. After each getchar (), manually clear the buffer. You can use the Fflush () function to clean up the buffer. the C standard specifies that the Fflush () function can be used to flush the output (stdout) buffer (typically, write buffer data back to the storage device). However, there is no explicit definition for standard input (stdin). Some compilers define the implementation of Fflush (stdin), such as Microsoft's VC. That is, different compilers may have different support for fflush (stdin). The GCC compiler does not define its implementation, so you cannot use Fflush (stdin) to flush the input buffer.
C language Buffer Problem--getchar description