Getchar () Usage

Source: Internet
Author: User
Getchar ()
1. reading a character from the buffer zone is equivalent to clearing the Buffer Zone 2. when reading the input, scanf () will leave a character '\ n' in the buffer (after entering the value of S [I], press the Enter key ), therefore, if you do not add a getchar () here to remove the carriage return, gets () will not wait for the character to be typed on the keyboard, but will directly remove the "useless" carriage return, as a result, the read error occurs. 3. getchar () is inconvenient to read a character (including spaces, carriage return, and Tab) in the input buffer sequence. Solution: (1) Clear the carriage return using the following statement: while (getchar ()! = '\ N'); (2) use getche () or getch () instead of getchar (). The function is to read a character from the keyboard (do not press Enter ), note that the header file <conio. h> 4. [convert] getchar () is stdio. the library function in H reads a character from the stdin stream. That is to say, if stdin has data, it can be directly read without entering it. For the first time getchar (), manual input is required, but if you lose multiple characters, later, when getchar () is executed, it will be directly read from the buffer. Actually: Enter the device-> memory buffer-> program getchar. The key you press is put into the buffer, for the program getchar, have you tried to hold down a lot of keys and wait for a while before Didi will ring, that is, the buffer is full, and the keys you press in the back are not saved in the buffer. All the characters entered by the keyboard are stored in the buffer. Once you press enter, getchar enters the buffer to read the characters. Only the first character is returned at a time as the value of the getchar function, if there are loops or enough getchar statements, all the characters in the buffer will be read in sequence until '\ n' (this' \ n' will also be read ). To understand this, the reason why you input a series of characters is read in sequence is that the function of loop allows the repeated use of getchar to read characters in the buffer, rather than getchar to read multiple characters, in fact, getchar can only read one character at a time. if you want to cancel the effect of '\ n', you can use getchar (); to clear it. Here, getchar (); only gets' \ n' but does not assign it to any character variable, so there is no effect, which is equivalent to clearing this character. note that the echo you see in ssss on the keyboard is exactly the function of getchar. If you use getch, you will not be able to see what you entered.

 

Getchar () Usage

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.