Getchar () and EOF Summary

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, the most typical getchar () program is just a few lines of code. 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
When using getchar for input, if the first character entered is a valid character, only when the last input character is a linefeed '/N' (or a file Terminator EOF, in Windows, press Ctrl + Z, Unix/Linux
Press Ctrl + d, and the EOF will be discussed later.) getchar stops running
Line, the entire program will be 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.

Pair
As for getchar, many beginners may ask, isn't getchar read in character units? So, since I entered the first character a, it must satisfy the while loop (C =
Getchar ())! =
For the 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 won't keep running.
Line, but must read a line break or file Terminator EOF for output.

One explanation of this problem is that when the master wrote C, there was no so-called terminal Input
All input is actually read according to the file, and the file is generally in the unit of action. Therefore, only when a line break is encountered, the program will think that the input is over and then take other
. 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:

Char
C;

C =
Getchar
(
)
;

This
The problem may occur. In addition to the characters entered by the terminal, the getchar function getchar ()
EOF, which 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 ):

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, press Ctrl + D when the first character is entered, and the getchar () can be jumped out to 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.

This
In this case, when a new row of getchar () is entered, when several characters are entered (cannot contain line breaks), press Ctrl + D, at this time, CTRL + D is not the end of the file.
It 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.
EOF
The function of getchar () can also be summarized as: when the terminal has a character input, 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; when the terminal has no character input or
It can be said that when getchar () reads a new input, press Ctrl + D. The generated EOF is equivalent to the file Terminator, and the program will end 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 only ends 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
(
)
;

/*

The Terminal Driver returns to the same line
*/

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.

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.