The use of EOF and GetChar () based on C language _c language

Source: Internet
Author: User

Master-Class classics, to read and understand carefully. I 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. It may be largely due to the lack of clarity about the workings of GetChar () and the use of EOF. Therefore, the feeling is necessary to sum up, otherwise, a lot of trivial knowledge points will be forgotten after a long time, 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 are the same.

First, the GetChar two points summary:
1.getchar is accessed in a behavioral unit.
When input is entered with GetChar, if the first character entered is a valid character (that is, the input is a combination key under the eof,windows of the file Terminator Ctrl+z, Unix/linux is the combination key ctrl+d), then only if the last input character is a line feed ' \ n ' ( Or it can be a file terminator eof,eof will be discussed later on, GetChar will stop executing and the entire program will be executed down. For example, the following program section:

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

Execute the program, input: ABC, and then enter. Then the program will execute Puchar (c), then output ABC, this place do not forget, the system output also has a carriage return. Then you can continue to enter, again encountered line break, the program will be that line of input characters output on the terminal.


For GetChar, certainly a lot of novice friends will ask, GetChar is not read in character units? So, since I entered the first character a, and certainly satisfies the condition of the while loop (c = GetChar ())!= EOF, then the Putchar (c) should be executed at the terminal output of a character a. Yes, I always think so when I use GetChar, but the program does not perform like a sample, but must read a line break or the end of the file to EOF for an output.

One explanation for this problem is that at the time the master wrote C, there was no notion of a terminal input, all inputs were actually read in accordance with the file, and the files were usually in the unit of behavior. Therefore, only if a line break is encountered, the program considers the input to end and then takes the rest of the execution program. At the same time, the input is accessed as a file, and EOF (Enf of file) is required to end the input of a document. This is why GetChar ends up with EOF when the input exits.

The return value of 2.getchar () is normally a character, but it can also be a negative value, that is, EOF is returned.

The point to be emphasized 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're going to write two lines of code like this:
char c;
c = GetChar ();

There is a good chance that the problem will arise. Because the GetChar function returns EOF for the GetChar (), which is generally defined as-1 in the function library, in addition to returning the characters entered by the terminal, when encountering Ctrl+d (Linux), the file terminator, EOF. Therefore, in this case, the GetChar function returns a negative value, and assigning a negative value to a char variable is incorrect. In order to allow the defined variable to contain all possible values returned by the GetChar function, the correct definition method is as follows (specifically mentioned in K&r C):
int C;
c = GetChar ();

Two, EOF two points summary (mainly refers to the ordinary terminal EOF)
1.EOF as a file terminator:

Although EOF is a file terminator, it is not under any circumstances entered Ctrl+d (Windows Ctrl+z) can implement the function of end of file, only under the following conditions, as a file terminator.
(1) When the Getcahr function is executed, the input of the first character is entered directly into the ctrl+d, and the other parts of the program can be carried out getchar ().
(2) Enter ctrl+d when the preceding character is a line feed;
(3) In the front has the character input and is not the line break character, must enter two times Ctrl+d, then the second input ctrl+d plays to the file terminator function, as for the first ctrl+d function will introduce below.
In fact, all three of these situations can be summed up as a direct input ctrl+d is equivalent to a file terminator only when GetChar () prompts for a new input.

2.EOF as a line terminator, the input ctrl+d does not end the GetChar (), but only GetChar () prompts the next round of input.

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

Note: The first set of ABC is input from the terminal, then input ctrl+d, output the second group of ABC, while the cursor stops in the second group of characters after C, and then can make a new input. At this point, if you enter Ctrl+d again, the end of the file will play a role, End GetChar ().
If you enter the ABC and then enter the line break, the terminal is displayed as:
ABC//First line, with carriage return
ABC//second line
Third line

One of the first behavior terminal input, the second behavior terminal output, the cursor stops at the third line, waiting for a new terminal input.
You can also see the different results of the output for ctrl+d and newline characters, respectively, as line Terminators.
The effect of EOF can also be summed up as follows: When the terminal has character input, the ctrl+d produces the EOF equivalent to the end of the line input, will cause getchar () new round of input, when the terminal has no character input or can say when GetChar () read a new input, input ctrl+d, The resulting EOF is the equivalent of a file terminator, and the program ends the execution of GetChar ().

"Supplemental" the summary section on EOF in Part two of this article applies to the terminal driver in one-line mode. That is, although the GetChar () and Putchar () are indeed based on one character at a time. But the terminal driver is in one line of mode, its input only to "\ n" or EOF to end, so the output on the terminal is also in line.
If you want to implement a terminal to read a character on the end of the input, the following program is a way to implement (refer to "C Expert programming", slightly changed)

Copy Code code 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");

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

/* Terminal driver back to a single line mode * *
System ("stty cooked");

return 0;
}


The compiler runs the program, then when a character is entered, a character is directly from the source, and then the program ends.
Thus, due to the different terminal-driven mode, the GetChar () input end of the condition is not the same. In normal mode, a carriage return or EOF is required, and in one character mode at a time, a character is entered and ends.

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.