Based on the C language EOF and getchar of the use of details

  • 2020-04-01 23:31:07
  • OfStack

Master class classic works, to read carefully, to understand. I was watching K&R's The C Programming Language(command edition)
The character input/output in section 1.5 is confused by getchar() and EOF. This is probably mainly due to not understanding how getchar() works and how EOF is used. Therefore, the feeling is very necessary to summarize, otherwise, a lot of trivial knowledge points will be forgotten after a long time, only write down is the best method.

The most typical getchar() program is just a few lines of code. The environment I use is DebianGNU/Linux, and the same is true on other systems.

One, getchar's two points summary:
Getchar is accessed as a behavior unit.
When use getchar for input, if the input of the first character for valid characters (at the end of the input is the file descriptor EOF, under Windows as the key combination Ctrl + Z, under Unix/Linux as the key combination Ctrl + D), and then only when the last one input for the newline character '\ n' (can also be the end of file descriptor EOF, EOF will be discussed later), getchar will stop the execution, the whole program will be down. For example:

While ((c = getchar ())! EOF) = {
      Putchar (c);
}

Execute the program, enter: ABC, then press enter. Then the program will go to puchar(c), and then output ABC, which is not to be forgotten, the system also output a return. Then you can continue typing, and when you encounter a newline character again, the program will output the characters of that line on the terminal.


For getchar, I'm sure many of my novice friends will ask, isn't getchar read in characters? So, since I typed the first character a, it must satisfy the while loop (c = getchar())! = EOF, then putchar(c) should be executed in the terminal output a character a. Yes, that's what I always thought when I used getchar, but instead of the sample line, the program must read a newline or EOF to output once.

One explanation for this problem is that when the master wrote C, there was no notion of terminal input, and all input was actually read in a file, usually in behavioral units. Because of this, only the newline character is encountered, the program will consider the input to be finished, and then take the rest of the program to execute. At the same time, the input is accessed as a File, so EOF (Enf Of File) is needed to end the input Of a File. This is why EOF is used when getchar finishes the input and exits.

The return value of getchar() is usually a character, but can also be negative, returning EOF.

The point to emphasize here is that the getchar function usually returns the characters entered by the terminal, and the corresponding ASCII values in the character system are all non-negative. So, a lot of times, we'll write two lines of code like this:
Char c;
C = getchar ();

There is a good chance that something will go wrong. Because getchar function in addition to return the terminal input characters, in the case of Ctrl+D(under Linux) that is the end of the file EOF, getchar () returns EOF, this EOF in the function library is generally defined as -1. Therefore, in this case, the getchar function returns a negative value, and it is not correct to assign a negative value to a variable of type char. In order for the defined variable to contain all possible values returned by the getchar function, the correct definition is as follows (this is specifically mentioned in K&R C) :
Int c;
C = getchar ();

Ii. Two-point summary of EOF (mainly refers to EOF in common terminal)
1. The situation when EOF is used as the end of the file:

EOF is the end of the file, but not in any case to enter Ctrl+D(Windows under Ctrl+Z) to achieve the function of the end of the file, only in the following conditions, as the end of the file.
(1) when the getcahr function is executed, to enter the first character directly input Ctrl+D, you can jump out of getchar(), to execute the rest of the program;
(2) when the character entered before is a newline character, then enter Ctrl+D;
(3) in front of the character input and not as a newline character, to be connected to the input twice Ctrl+D, then the second input of Ctrl+D to play the function of the end of the file, as for the first Ctrl+D role will be described in the following.
In fact, all three cases can be summarized as simply entering Ctrl+D equals the end of the file only when getchar() prompts for a new input.

2. When EOF is a line terminator, entering Ctrl+D does not end getchar(), but only causes getchar() to prompt for the next round of input.

This situation is mainly in the getchar() new line input, when the input of several characters (can not contain the line break), directly enter Ctrl+D, Ctrl+D is not the end of the file, but just equivalent to the function of the line break, that is, the end of the current input. Take the above code snippet as an example. If ABC is input during execution, then Ctrl+D, the output result of the program is:
abcabc

Note: the first set of abcs is input from the terminal, then input Ctrl+D, output the second set of abcs, while the cursor over the second set of characters after the c, then can make a new input. If you type Ctrl+D again, you end getchar ().
If you enter ABC and then press enter to enter the newline character, the terminal will be displayed as:
ABC // first line, bring back the car
ABC // second row
/ / the third row

The first behavior terminal input, the second behavior terminal output, the cursor stopped at the third line, waiting for a new terminal input.
From here you can also see the different output results when Ctrl+D and newline are used as line endings respectively.
EOF can also be summarized as: when the terminal has character input, Ctrl+D produces EOF equivalent to end line input, will cause getchar() a new round of input; When the terminal has no character input or when getchar() reads a new input, enter Ctrl+D, and the EOF is the equivalent of the file terminator, and the program will end the execution of getchar().

[supplement] the summary of EOF in the second part of this paper is applicable to the terminal driver in one-row mode. That is, although getchar() and putchar() do work one character at a time. But the terminal driver is in line at a time mode, and its input only ends when it reaches "\n" or EOF, so the output on the terminal is also in line.
If you want to achieve the terminal read a character to end the input, the following program is a method of implementation (see "C expert programming", slightly changed)


/*Edit by Godbach
    CU Blog: http://blog.chinaunix.net/u/33048/
 */
 #include <stdio.h>
 #include <stdlib.h>
 int 
 main(void)
 {
    int c;
    
    system("stty raw");
    
    c = getchar();
    putchar();
    
    system("stty cooked");
    return 0;
 }

Compile to run the program, when such as a character, a direct source of a character, and then the program ends.
Thus, the conditions for the end of the getchar() input are different due to the different terminal driver modes. Normal mode requires a return or EOF, but in one character at a time mode, you enter one character and end.


Related articles: