GetChar () and EOF

Source: Internet
Author: User

Master Classics of the book, to carve to read, to understand. I used to look at the character input/output of section 1.5 of the C programming Language (Second Edition) in K&r, confusing the behavior of GetChar () and 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.

First, the two points of the GetChar summary:


1. GetChar is accessed in a behavioral unit.
When the GetChar function is called to read the input, the GetChar stops execution only if the input character is the line feed '/n ' or the file terminator eof, and the entire program executes down. Also, if the input line ends with EOF (EOF is not a newline character), EOF is "eaten" (that is, it is not read by GetChar). For example, the following program section:

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

Execute the program, enter: ABC, and then enter. The program executes Puchar (c), then outputs ABC and a carriage 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. What is confusing is that GetChar is not read in a character unit? Well, since I entered the first character a and certainly satisfy the condition of the while loop (c = GetChar ())! = EOF, then you should do Putchar (c) to output a character a at the terminal. However, the program does not do so, but must read a newline character or file terminator eof to do the output once.

One explanation for this result is that the input terminal driver is in a one-line 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 ends only when it is '/n ' or EOF. In this case, the program segment calls the GetChar function, then the control is transferred from the program segment to the GetChar function, and the GetChar function relies on the operating system's driver to read the input, without encountering a newline or EOF, the driver does not notify the GetChar function, and the GetChar function is "blocked Plug "status. After encountering a newline character or EOF, the GetChar function unlocks "block", reads a character, controls the return program segment, executes the Putchar function, and loops through the execution. Until the EOF character is encountered or the line input is all processed.


2. The return value of GetChar () is generally non-negative, but can also be negative, which is to return EOF. This EOF is generally defined as-1 in the Library of functions. The correct definition method is as follows (K&r C specifically mentions this issue):

int C;
c = GetChar ();

Ii. Two summary of EOF (mainly referred to as EOF in the normal terminal)
1. When EOF is used 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) When encountering Getcahr function execution, enter the first character to enter the Ctrl+d directly;
(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 file terminator, as for the first time
Ctrl+d as the line terminator (as in 1.1).


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. When EOF is used as a line terminator, entering Ctrl+d as the end of the line can end the "blocking" of GetChar (), allowing GetChar () to read the characters one by one, but EOF will be "eaten" and will not be read.

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 the one you entered from the terminal, and then input Ctrl+d,getchar read-by-character and print out the second set of ABC on a per-output basis, while the cursor stops behind the C of the second set of characters and can then make a new input. If you enter Ctrl+d again, the file terminator will function because EOF is the first character entered in a line. If you enter ABC and then enter, entering a newline character, the terminal appears as:
ABC '/n '
ABC '/n '
Third line

The first behavior is that you are the terminal input, the second line is the terminal output (including newline characters), 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.

Transferred from: http://www.cnblogs.com/QLinux/articles/2465329.html

GetChar () and EOF

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.