A detailed description of the use of the C-language EOF and GetChar ()

Source: Internet
Author: User

Transferred from: http://www.jb51.net/article/36848.htm

Master Classics of the book, to carve to read, to understand. Used to look at K&r's the C Programming Language (Secondedition)
The character input/output of section 1.5 is confused by GetChar () and EOF. Probably largely due to the lack of clarity on how GetChar () works and the use of EOF. Therefore, it is necessary to summarize the feeling, otherwise, a lot of trivial knowledge points after a long time will be forgotten, only write down is the best way.

In fact, GetChar () The most typical program is just a few lines of code. I use the environment is debiangnu/linux, in other systems also the same.

First, GetChar's two points summary:
1.getchar is accessed as a unit of behavior.
When input with GetChar, if the first character entered is a valid character (that is, the input is the file terminator eof,windows under the key combination of CTRL + Z, Unix/linux under the key combination Ctrl+d), then only if the last input character is the line break ' \ n ' ( It can also be the file Terminator eof,eof will be discussed later), GetChar will not stop execution, the entire program will be executed down. For example, the following program section:

while ((c = GetChar ())! = EOF) {
Putchar (c);
}

Execute the program, enter: ABC, and then enter. The program will execute Puchar (c), and then output ABC, this place do not forget, the system output has a return. You can then continue typing, and again, when you encounter a newline character, the program will output the input characters of that line to the terminal.

For GetChar, certainly a lot of novice friends will ask, GetChar is not read in the character unit? Well, since I entered the first character a, definitely satisfies the condition of the while loop (c = GetChar ())! = EOF, then you should do Putchar (c) to output a character a in the terminal. Yes, I have always thought of this when using GetChar, but the program does not have to do the kind of execution, but must read to a newline character or the file terminator eof before the output.

One explanation for this problem is that when the master wrote C, there was no concept of terminal input at that time, all the input was actually read according to the file, and the file was usually in the unit of behavior. Therefore, only a newline character is encountered, then the program considers the input to be finished and then takes the other part of the execution program. At the same time, the input is accessed as a file, so the input to end a file needs to be EOF (Enf of file). That's why GetChar ends up with EOF when the input exits.

The return value of 2.getchar () is generally a character, but it can also be a negative number, which is the return of EOF.

One thing to emphasize here is that the GetChar function usually returns the characters entered by the terminal, and the corresponding ASCII values in these character systems are 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 there will be a problem. Because the GetChar function returns EOF, which is generally defined as 1 in the function library, in addition to the characters entered by the terminal, when encountering Ctrl+d (Linux), or the file terminator, EOF, is the EOF of GetChar (). Therefore, in this case, the GetChar function returns a negative value, and assigning a negative value to a variable of type char is incorrect. In order to allow the defined variable to contain all possible values returned by the GetChar function, the correct definition is as follows (this problem is specifically mentioned in K&r C):
int C;
c = GetChar ();

Ii. Two summary of EOF (mainly referred to as EOF in the normal terminal)
1.EOF as a file terminator:

Although EOF is a file terminator, it is not possible to enter Ctrl+d (Windows Ctrl + Z) in any case to achieve end-of-file functionality, only as a file terminator if the following conditions are true.
(1) Encountered Getcahr function execution, to enter the first character when the direct input ctrl+d, you can jump out of the GetChar (), to execute the other parts of the program;
(2) When the character entered earlier is a line break, enter ctrl+d;
(3) in the preceding character input and is not a newline character, to input two times ctrl+d, then the second input of the Ctrl+d play the function of the end of the file, as for the first time the role of Ctrl+d will be described below.
In fact, these three cases can be summed up as only when the GetChar () prompt for a new input, the direct input ctrl+d is equivalent to the file terminator.

2.EOF as a line terminator, this time input ctrl+d can not end GetChar (), but only the GetChar () prompt the next round of input.

This is mainly when GetChar () a new line of input, when the input of a number of characters (cannot contain newline characters), the direct input ctrl+d, at this time the ctrl+d is not a file terminator, but just equivalent to the function of newline character, that is, the end of the current input. As an example of the above code snippet, if you enter ABC at execution time and then Ctrl+d, the program outputs the result:
Abcabc

Note: The first set of ABC is entered from the terminal, then input ctrl+d, the second set of ABC output, while the cursor stops at the second set of characters after the C, and then you can make a new input. If you enter ctrl+d again, it acts as a file terminator, ending GetChar ().
If you enter ABC and then enter, entering a newline character, the terminal appears as:
ABC//First line, with carriage return
ABC//second line
Third line

Where the first behavior terminal input, the second behavior terminal output, the cursor stops at the third line, waiting for a new terminal input.
From here you can also see the different results of the output when the Ctrl+d and newline characters are used as line terminators, respectively.
The role of EOF can also be summed up as: When the terminal has character input, the ctrl+d generated EOF equivalent to the end of the bank's input, will cause a new round of getchar () input; When the terminal does not have character input or can say when GetChar () reads a new input, the input ctrl+d, The resulting EOF is equivalent to the file terminator, and the program ends execution of GetChar ().
"Supplemental" The summary of EOF in the second part of this article applies to the terminal driver in a single row mode. That is, although GetChar () and Putchar () are indeed performed by one character at a time. But the terminal driver is in a one-line mode, and its input only ends with "\ n" or EOF, so the output from the terminal is also on line.
The following program is an implementation method (refer to "C Expert programming" for a slight change) if the terminal is to be entered at the end of reading a single character.

The code is as follows:

/*edit by Godbach
CU blog:http://blog.chinaunix.net/u/33048/
*/
#include <stdio.h>
#include <stdlib.h>

Int
Main (void)
{
int C;
/* Terminal driver in a normal one-line mode */
System ("Stty raw");

/* Current terminal driver is in one character mode at a time */
c = GetChar ();
Putchar ();

/* Terminal driver goes back one line mode */
System ("stty cooked");

return 0;
}

Compile and run the program, then when a character is entered, a character is directly sourced, and then the program ends.
Thus, due to the different terminal-driven modes, the GetChar () input end condition is not the same. In normal mode, a carriage return or EOF is required, and in a single character mode, a character is entered and then the end is completed.

A detailed description of the use of the C-language EOF and GetChar ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.