Getch (), getche (), and getchar () Functions

Source: Internet
Author: User
Tags clear screen
(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 */
}

Getch () is a character that is waiting to be read from the keyboard and is not displayed on the screen.

Getche () display and on-screen

If you want to use GETCH () when using TC in WIN, include this header file: <conio. h>

Use this package # include <curses. h> in LINUX. If an error is reported during connection compilation, run the following command: (gcc-lncurses)
For example, gcc-o telephonum-lncurses telephonum. c

In addition:
// Clrscr (); clrscr is unique to TC. This function is not available in other C Language Environments. The function is clear screen!
System ("cls"); // system ("cls"); to replace clrscr (); more common, better compatibility. The system () function is in # include <stdlib. h>.
Printf ("/tADD new subscriber details ");

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.