Linux under C Programming: Getch () detailed

Source: Internet
Author: User
Tags function prototype linux

Getch ()

The header file: conio.h

function Purpose: Read a character from the console

Function prototype: int getch (void)

Return value: The character read

For example:

char ch; or int ch;

Getch (); or ch=getch ();

Use Getch (), wait for you to press any key, then continue to execute the following statement;

With Ch=getch (), wait for you to press any key, assign the ASCII code corresponding to the key character to CH, and then execute the following statement.

Easy to mistake Point:

1. The header file is conio.h. Rather than stdio.h.

2. Call INITSCR () before use, and call Endwin () at the end. Otherwise, this function will appear without entering characters.

will also return the case.

3. On different platforms, enter a return, Getch () will return a different value, and GetChar () uniform return 10 (that is \ n)

1 The Windows platform under the ENTER key produces two escape characters \ r \ n, so Getch returns (\ r).

2 the Enter key in Unix and Linux only produces \ n, so getch returns ten (\ n).

3 the ENTER key in MAC OS will generate \ r, so Getch returns (\ r).

Getch () is not a function in standard C and does not exist in C language. So in the use of the time to pay attention to the portability of the program. Domestic C language Novice often use getch (), to suspend the program and do not know the source of this function, it is recommended to use GetChar (), (if circumstances allow) to replace this feature or to replace a compiler.

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.