Getchar and EOF Summary (Mark zz)

Source: Internet
Author: User
For classic masters, you must read and understand them carefully. I used to check the C programming language (secondedition) of K & R)
The character input/output in section 1.5 is confused by getchar () and EOF. The reason may be that the working principle and EOF usage of getchar () are not clear. 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.

Actually, getchar () is the most typicalProgramJust a few linesCode. My environment is debiangnu/Linux, and the same is true in other systems.
I. Two points of getchar summary:
1. getchar is accessed in the unit of behavior.
When getchar is used for input, if the first character entered is a valid character (that is, the input is the file Terminator EOF, Windows is the combination key Ctrl + Z, in Unix/Linux, press Ctrl + D. Only when the last input character is the linefeed '\ n' (or the file Terminator EOF, which will be discussed later, getchar stops execution and the entire program is executed. For example, the following program section:

while ( ( C = getchar ( ) ) ! = EOF ) {
putchar ( C ) ;
}

Run the program, enter ABC, and press Enter. Then the program will execute puchar (c) And then output ABC. Do not forget this. The system outputs 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.

For getchar, many beginners may ask, isn't getchar 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. Yes, I keep thinking this way when using getchar, but the program does not execute like a sample. Instead, it must read a linefeed or file Terminator EOF to output it once.

One explanation of this problem is that when the master wrote C, there was no so-called terminal input concept at the time, and all input was actually read according to the file, files are generally behavior units. Therefore, only when a line break is encountered, the program considers the input to end and then takes other parts of the execution program. At the same time, the input is accessed by file, so to end the input of a file, you need to use EOF (enf of file ). this is why EOF is used when the getchar end input exits.

2. the return value of getchar () is generally a character, but it may also be a negative value, that is, EOF is returned.

It should be emphasized that the getchar function usually returns the characters entered by the terminal. the ASCII values in these character systems are non-negative. Therefore, we often write the following two lines of code:

CharC;
C= Getchar();

In this way, problems may occur. In addition to the characters entered by the terminal, the getchar function returns EOF when Ctrl + d (in Linux) is the file Terminator EOF, this EOF is generally defined as-1 in the function library. Therefore, in this case, the getchar function returns a negative value and it is incorrect to assign a negative value to a char variable. To allow the Defined variables to include all possible values returned by the getchar function, the correct definition method is as follows (this problem is not mentioned in K & r c ):

IntC;
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 getchar function is executed, press Ctrl + D to enter the first character to jump out of getchar () and execute other parts of the program;
(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, enter Ctrl + d twice in a row. Then, the CTRL + D entered for the second time serves as the end character of the file, the role of Ctrl + D for the first time will be described below.
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 does not end getchar (), but only triggers getchar () to prompt the next round of input.

In this case, when a new row of getchar () is input, when a certain number of characters (cannot contain line breaks) are input, press Ctrl + D, at this time, CTRL + D is not the end of the file, but is equivalent to the line break function, that is, to end the current input. 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 input from the terminal, and then Ctrl + D is input, the second group of ABC is output, and the cursor stops behind the second group of characters C, and then a new input can be made. If Ctrl + D is entered again, the end of the file is ended with getchar ().
If you enter ABC, press enter, and enter a line break, the terminal will display:
ABC // The first line, with carriage return
ABC // The second line
// The third line

The first act is terminal input, the second act is terminal output, and the cursor stops at the third line, waiting 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.
The role of EOF can also be summarized as: when there is a character input in the terminal, the EOF generated by Ctrl + D is equivalent to ending the input of this line, which will lead to a new round of input of getchar; if the terminal does not have a character input, or you can say that when getchar () reads a new input, press Ctrl + D. The generated EOF is equivalent to the file Terminator, and the program ends the execution of getchar.
[Supplement] The summary of EOF in the second part of this article is applicable to the mode where the terminal driver is in one row. That is, although getchar () and putchar () are exactly one character at a time. However, the terminal driver is in the one-row mode, and its input ends only when "\ n" or EOF. Therefore, the output obtained on the terminal is also row-based.
If you want to enable the terminal to end the input after reading a character, the following Program is an implementation method (refer to "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 ;
/* The Terminal Driver is in normal one-row mode */
System ( "Stty raw" ) ;

/* The current terminal driver is in the one-Character Mode at a time */
C= Getchar ( ) ;
Putchar ( ) ;

/* Return to the One-row mode again at the terminal driver */
System ( "Stty cooked" ) ;

Return 0 ;
}

Compile and run the program. When a character is entered, the program will start with one character and end.
It can be seen that the conditions for the end of the getchar () input are different due to different terminal-driven modes. In normal mode, you need to press ENTER or EOF, while in one character at a time, it will end after one character is entered.

I hope this article can help anyone who is new to C and communicate with others. I am very grateful for the correction and suggestions for any mistakes I have made. At the same time, this article references chinaunix.net's post on getchar andArticle, The Link addresses are:
Http://blog.chinaunix.net/u/9861/showart_64652.html
Http://bbs.chinaunix.net/viewthread.php? Tid = 679688 & extra = & page = 1
Thank you for your comments and comments. Http://blog.chinaunix.net/u/33048/showart_271120.html

 

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.