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 */
}