Linux C character Functions GetChar (), Putchar () and EOF

Source: Internet
Author: User

First, give an example of the book "The_c_programming_language":

#include <stdio.h>int main () {int c;c = GetChar (); while (c! = EOF) {Putchar (); c = GetChar ();} return 0;}

The main explanation here is why you should use the int type to accept the GetChar function.

Many times, we will write two lines of code:

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.

Here's a topic to look at how to get characters and output characters.

#include "stdio.h" main () {char C, D, E, f;printf ("Please input, characters:\n"), C = GetChar ();p Utchar (c);p Utchar (' \ n ') ;d = GetChar ();p Utchar (d);p Utchar (' \ n '), E = GetChar ();p Utchar (e);p Utchar (' \ n '), F = GetChar ();p Utchar (f);p Utchar (' \ n ') ;p rintf ("c=%c\n", c);p rintf ("d=%c\n", D);p rintf ("e=%c\n", E);p rintf ("f=%c\n", f);}

After running, enter "12", enter, then "34", return.

Operating environment is CentOS GCC

Operation Result:

Please input the characters:
12
1
2

34
3
C=1
d=2
E=

F=3

Here is a specific explanation:

Each time the GetChar function obtains a character from the buffer, the Putchar function outputs one character at a time.

First enter two characters 12, and then enter, note that at this time the write cache has 3 characters 1, 2, enter.

There are four GetChar () in the program, so c= ' 1 ', d= ' 2 ', e= ' \ n '.

Then run to F=getchar (); The three characters in the input cache are obtained by the first three GetChar, which requires user input,

We have entered 34

So the f=3,4 and the rear carriage return were not used.

This is the whole process.

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

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 in a behavioral unit.

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:

int C;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 at the terminal. Yes, I have always thought of this when using GetChar, but the program does not do so, but must read a newline character or the file terminator eof to output once.

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), which is why GetChar ends with EOF when the input exits.

2, the return value of GetChar () is usually a character, but may also be a negative, that is, return 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. 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), 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 in the preceding line is newline character, then enter Ctrl+d;

(3), in front of the character input and is not a newline character, to input two times ctrl+d, then the second input of the ctrl+d up to the function of the file terminator, 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 when the case, this time input ctrl+d can not end GetChar (), and can only throw 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

Attention:

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 ().

Added

The summary of EOF in the second part of this article applies to the terminal driver 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 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.

#include <stdio.h> #include <stdlib.h>int main (void) {int c;//terminal Drive in a normal one-line mode system ("Stty raw");// Now the terminal driver is in one character mode at a time C = GetChar ();p Utchar (c);//terminal driver goes back one line mode system ("stty cooked"); return 0;}

Reference:

Http://www.cnblogs.com/hdchild/archive/2009/11/19/1606457.html

"C Traps and defects" chap5.1
Http://www.cnblogs.com/younes/archive/2010/05/31/1748002.html

Linux C character Functions GetChar (), Putchar () and EOF

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.