[C Language] console input principle of the getchar function, handling carriage return

Source: Internet
Author: User

The function of the getchar function reads a character from the input stream, which should be clear:

We need to enter at least two characters in the console to ensure the execution of this function:

For example:

Source program:

Char ch;

Ch = getchar ();

We need to enter "w press ENTER" in the standard input stream, that is, we need to enter at least two characters. One is a normal character accepted, and the other is a carriage return line break. This carriage return function is like a trigger, as long as you enter the carriage return source program, it will be executed once. If you enter enough numbers at a time in the input stream, you will not need to enter them in the future, because all functions in the source program that accept the input stream will automatically read data from the input stream buffer.

 


For example, a baidu user asked a very good question:


[Html]
<SPAN style = "FONT-SIZE: 12px"> scanf ("% c", & a); // enter a character for
Gets (B); // enter a string to B
Gets (c); // input a string to c
When I enter a character to a, press ENTER
How can I directly jump to the input string c?
After debugging, it will automatically input the character '\ 0' to B after entering'
Why ?? </SPAN>

Scanf ("% c", & a); // enter a character for
Gets (B); // enter a string to B
Gets (c); // input a string to c
When I enter a character to a, press ENTER
How can I directly jump to the input string c?
After debugging, it will automatically input the character '\ 0' to B after entering'
Why ??
The reason is that two characters are entered at the beginning, and the carriage return triggers the program running. After the program completes reading the first character, it assigns the first character to the scanf function, then the carriage return is read by gets (B), and then the buffer zone is empty. This naturally requires the user to input characters from the input stream and end with a carriage return.

The getchar function is the same as the scanf function.

 


Currently, I know two solutions:

1. You can add a function to the two functions to read the carriage return, getchar ();

2 if it is a scanf () function, you can directly write the return letter scanf ("% c \ n", & a); it is mainly to add the \ n following the format string. As for the principle, I still can't say it clearly, but it may be that two linefeeds appear at the same time, so the compiler will automatically remove one. This issue is still under exploration...

 


 

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.