[C tips] getch (), getche () and getchar ()

Source: Internet
Author: User
Getch (), getche (), and getchar () Functions

(1) getch () and getche () Functions
Both functions read one character from the keyboard. The call format is:
Getch ();
Getche ();
The difference between the two is that the getch () function does not display the characters read back on the display screen, while the getche () function does ()
The function returns the characters read to the display screen.
Example 1:
# Include <stdio. h>
Main ()
{
Char C, ch;
C = getch ();/* read a character from the keyboard and do not return it to the character variable C */
Putchar (c);/* output this character */
Ch = getche ();/* read a character explicitly from the keyboard and send it to the character variable Ch */
Putchar (CH );
}
With the features of ECHO and non-Echo, these two functions are often used to complete the pause during interactive input.
.
Example 2:
# Include <stdio. h>
Main ()
{
Char C, S [20];
Printf ("name :");
Gets (s );
Printf ("press any key to continue ...");
Getch ();/* wait for any input key */
}

(2) getchar () function
The getchar () function also reads a character from the keyboard and brings it back to the display. It corresponds to the first two functions.
The difference is: The getchar () function waits for the input to end until Press enter. All input words before press ENTER
Are displayed on the screen one by one. But only the first character is used as the return value of the function.
The call format of the getchar () function is:
Getchar ();
Example 3:
# Include <stdio. h>
Main ()
{
Char C;
C = getchar ();/* read characters from the keyboard until the carriage return ends */
Putchar (c);/* display the first character of the input */
Getch ();/* wait for any key */
}
Example 4
# Include <stdio. h>
Main ()
{
Char C;
While (C = getchar ())! = '\ N')/* Each getchar () reads one character in sequence */
Printf ("% C", c);/* output as is */
Getch ();/* wait for any key */
}

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.