Original: Getch (), Getche (), and GetChar () functions
Getch (), Getche (), and GetChar () functions
(1) Getch () and Getche () functions
Both of these functions read a character from the keyboard. Its invocation format is:
Getch ();
Getche ();
The difference is that the getch () function does not echo the read-in character on the display screen, while the Getche () function echo the read-in Word to the display screen
On
Example 1:
1#include"stdio.h"
2#include"conio.h"
3 voidMain ()
4{
5 Charc, ch;
6C=getch ();/*read a character from the keyboard without echoing to the character variable C*/
7Putchar (c);/*Output this character*/
8Ch=getche ();/*reads a character from the keyboard to the character variable ch*/
9Putchar (CH);
Ten}
With Echo and non-ECHO features, these two functions are often used to perform functions such as pausing during interactive input.
Example 2:
1#include"stdio.h"
2#include"conio.h"
3 voidMain ()
4{
5 Chars[ -];
6printf"Name:");
7Gets (s);
8printf"Press any key to continue ...");
9Getch ();/*wait for any key to enter*/
Ten}
(2) GetChar () function
The GetChar () function also reads a character from the keyboard and brings back the display. It differs from the previous two functions in that: the GetChar () function waits for input straight
Until you press ENTER to end, all input characters before carriage return are displayed on the screen one by one. But only the first character is the return value of the function.
Example 3:
- #include "stdio.h"
- #include "conio.h"
- void Main ()
- {
- char c;
- C=getchar (); /* Read characters from keyboard until end of carriage return */
- Putchar (c); /* Displays the first character of the input */
- Getch (); /* wait to press Ning */
- }
Example 4:
- #include "stdio.h"
- #include "conio.h"
- void Main ()
- {
- char c;
- while ((C=getchar ()) = ' \ n ') /* per GetChar () Read in one character */
- printf ("%c", c); /* Output AS-is */
- getch (); / * Wait for any health */
- }
Programming, in case the program runs a flash is lost, always in the end to a "getch ();"
Getch () and Getche () require "conio.h"
Report:
Getch read one character from the keyboard without echoing back
Call format #include "stdio.h"
int getch (void);
Prototype in Conio.h
Description Getch reads a character directly from the keyboard and does not echo back to the screen.
The return value Getch returns the characters that are read from the keyboard.
Portability Getch only available for DOS
GetChar to take characters from the stdin stream
Call format #include "conio.h"
int GetChar (void);
Prototype in Stdio.h
Description GetChar is a macro that returns the next character of the specified stdin input stream, which is defined as GETC (stdin).
When the return value call succeeds, GetChar returns the read character, which has been converted to an integer value for the unsigned extension. When you encounter a file end or an error,
Returns EOF.
Portability GetChar for UNIX systems, defined in ANSI C, compatible with Kernighan and Ritchie definitions
Getch (), Getche (), and GetChar () functions