This article is based on: http://www.cnblogs.com/qlinux/articles/2465329.html.
For classic masters, you must read and understand them carefully. The character input/output in section 1.5 of K & R's C programming language (second edition) is confusing with getchar () and EOF. Therefore, it is necessary to sum up. Otherwise, many trivial knowledge points will be forgotten after a long time. Only writing down is the best method.
I. Summary of getchar:
1. getchar is accessed in the unit of behavior.
When the getchar function is called to read input data, getchar stops execution only when the input character is '/N' or the file Terminator EOF. The entire program is executed. In addition, if the input line ends with EOF (EOF is not a line break before), EOF will be "eaten" (that is, it will not be read by getchar ). For example, the following program section:
While (C = getchar ())! = EOF ){
Putchar (C );
}
Execution Program
Enter ABC and press Enter. Then the program will execute puchar (C), and then output ABC and a carriage return. Then you can continue the input. When you encounter a linefeed again, the program will output the input characters of that line on the terminal.
Input: ABC, and then execute Ctrl + d, then the program area executes putchar (C), and then outputs ABC. (CTRL + D has the same function as press Enter ).
What is confusing is that getchar is not read in character units? Now that I have entered the first character "A", it must satisfy the while loop (C = getchar ())! = EOF condition, you should execute putchar (c) to output a character a on the terminal. However, the program does not execute this way, but must read a line break or file Terminator EOF to output it once.
One explanation of this result is that the input terminal driver is in a row-by-row mode. That is, although getchar () and putchar () are based on one character each time. However, the terminal driver is in the one-row Mode and Its input only ends when '/N' or the EOF. In this example, if the program segment calls the getchar function, the control is transferred from the program segment to the getchar function. The getchar function depends on the operating system driver to read the input without line breaks or EOF, the driver does not notify the getchar function. The getchar function is in the "blocking" status.
Plug status. In the event of a linefeed or EOF, The getchar function removes the "blocking" and reads a single character. The control class returns the procedural segment, executes the putchar function, and executes it cyclically. Until all the EOF characters or input in this line are processed.
2. The returned value of getchar () is generally a non-negative value, but it may also be a negative value, that is, EOF is returned. This EOF is generally defined as-1 in the function library. The correct definition method is as follows (this problem is not mentioned in K & r c ):
Int C;
C = getchar ();
Ii. Two points of EOF (EOF in general terminals)
1. When EOF is used as the file Terminator:
Although EOF is a file Terminator, it does not enable the file termination function by entering Ctrl + d (CTRL + Z in Windows) under any circumstances, is used as the file Terminator.
(1) When the getcahr function is executed, enter Ctrl + D when the first character is required;
(2) When the character entered earlier is a line break, press Ctrl + D;
(3) When there is a character input before and it is not a line break, you must enter Ctrl + d twice. Then the CTRL + D entered for the second time serves as the end character of the file.
CTRL + D as the row terminator (as described in 1.1 ).
In fact, all three cases can be summarized as that only when getchar () prompts a new input, directly entering Ctrl + D is equivalent to the file Terminator.
2. when EOF is used as the row Terminator, entering Ctrl + D as the row ending sign can end the "blocking" of getchar () so that getchar () can be read one by one, however, the EOF will be "eaten" and will not be read.
The preceding code segment is used as an example. If ABC is input during execution and CTRL + D is input, the output result of the program is:
Abcabc
Note: the first group of ABC is entered from the terminal. Then, enter Ctrl + D and getchar to read and output the second group of ABC one by one. Meanwhile, the cursor stops behind the second group of C, then, you can perform a new input. At this time, if you enter Ctrl + D again, it will play the role of the file Terminator, because EOF is the first character entered in a line. If you enter ABC, press enter, and enter a line break, the terminal will display:
Abc'/N'
Abc'/N'
// The third line
The first line is terminal input, and the second line is terminal output (including line breaks). The cursor stops at the third line and waits for a new terminal input. We can also see the different output results when Ctrl + D and linefeed are used as the line terminator respectively.