The difference between Getch and GetChar under Windows and UNIX

Source: Internet
Author: User

Note that getch () is read from the console (non-standard function)
Getch () required header file is <conio.h>.
and GetChar () is from stdin, generally refers to the keyboard
The ENTER key on the Windows platform generates two escape characters \ r \ n,
\ r: Back to the beginning of the place.
\ n: Go to the beginning of the next line



Therefore, Getch () will return his ASCII code 13 when it reads \ r. (the back is ignored)
The odd question is why GetChar () will return 10?
Didn't you say that the first character is returned?
This is really confusing. In fact, the reason for this result is that
GetChar () converts the input \ r \ n to \ n,
So the return is \ n ASCII code 10. Why is that?
Because the previous getchar () is a C standard library function,
In the UNIX system, the ENTER key only generates \ n. By the way,
The ENTER key in Mac OS will generate \ R and will also be replaced by \ n.
In this way, GetChar () will get the same results, regardless of the platform,
So say GetChar () standard library functions.

Http://www.cnblogs.com/candyming/archive/2011/11/25/2262877.html

The difference between Getch and GetChar under Windows and UNIX

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.