Re-discussion of Getch () and GetChar ()

Source: Internet
Author: User

The original: Getch () and GetChar () re-discussion

In the C language character handler, Getch () and GetChar () are often confusing two functions, and they all have "strange" features that make beginners feel confused. There are a lot of similarities between the two functions, but they are very different. The following is a description of the two functions:

int getch (void); Get a character from the console without echo

int getchar (void);   get character from stdin, Returns t He next character from the standard input (stdin). It is equivalent to getc with stdin as its argument

, you can see that two functions have no parameters, are read in a character, the return value is an int type. But note that getch () is read from the console, while GetChar () is from stdin, which is generally referred to as the keyboard. To test the difference between the two, you can write a getch () to see if you do not input from the keyboard, such as copying a character directly to the console with the mouse, you will see that Getch () will return immediately, but GetChar waits until the ENTER key is encountered.

They also have the following differences:

First, GetChar () is the standard library function for C, contained in header file <stdio.h>, and Getch () requires <conio.h>.

Second, getch () reads a character to return, regardless of the character, so in the TC era is often placed at the back of the program, reaching the "Press any key exit" effect. The GetChar () is not returned until the ENTER key is met, the return value is the first character, but the subsequent character is not discarded, but is stored in a buffer.

A few days ago, someone in the group asked the difference between getch () and GetChar () because he returned 13 after typing enter, and the latter returned 10. The code is as follows:

    1. #include <stdio.h>
    2. #include <conio.h>
    3. int main ()
    4. {
    5. int ch, cha;
    6. ch = getch ();
    7. Cha = GetChar ();
    8. printf ("ch=%d, cha=%d\n", Ch,cha);
    9. Getch ();
    10. return 0;
    11. }
    12. Enter ENTER
    13. Ch=13, cha=10
Copy Code

In particular, this code is under the Windows platform to produce the difference as described above. The reason is that the ENTER key on the Windows platform generates two escape characters \ r \ n, so Getch () will return his ASCII code 13 when read to \ r.

The odd question is why GetChar () will return 10? Didn't you say that the first character is returned?

This is really confusing. The result is actually generated because GetChar () converts the input \ r \ n to \ n, so it returns the ASCII code 10. Why is that? As mentioned earlier GetChar () is a C standard library function, whereas the ENTER key in a UNIX system only produces \ n. By the way, the ENTER key in Mac OS will generate \ R and will also be replaced by \ n. Thus, GetChar () will get the same result regardless of the platform, so GetChar () standard library function.

Re-discussion of Getch () and GetChar ()

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.