Format
#include <conio.h> int _getch(void); int _getche(void);
Language Level: Extension
_getch reads a single character from the keyboard,
without echoing. _getche reads a single character from the
keyboard and displays the character read. Neither function can be
used to read Ctrl-Break.
You can use _kbhit to test if a keystroke is waiting in the buffer. If you call _getch or _getche without first calling _kbhit, the program waits for a key to be pressed.
Return Value
_getch and _getche return the character
read. To read a function key or cursor-moving key, you must call
_getch and _getche twice; the first call returns 0 or 0xE0, and
the second call returns the particular extended key code.
Example
This example gets characters from the
keyboard until it finds the character 'x'.
#include <conio.h> #include <stdio.h>
int main(void)
{
int ch;
printf("\nType in some letters.\n");
printf("If you type in an 'x', the program ends.\n");
for(;;) {
ch = _getch();
if ('x' == ch) {
_ungetch(ch);
break;
}
_putch(ch);
}
ch = _getch();
printf("\n");
printf("\nThe last character was '%c'.", ch);
return 0;
/*****************************************************
Here is the output from a sample run:
Type in some letters.
If you type in an 'x', the program ends.
One Two Three Four Five Si
The last character was 'x'.
*****************************************************/
}
![]()
_cgets -- Read String of
Characters from Keyboard
fgetc -- Read a
Character
getc - getchar --
Read a Character
_kbhit -- Test
for Keystroke
_putch -- Write
Character to Screen
_ungetch -- Push
Character Back to Keyboard
<conio.h>