_getch, _getwch, _getche, _getwche
Get a character from the console without echo (_getch, _getchw) or with echo (_getche, _getwche).
int _getch( void ); wint_t _getwch( void ); int _getche( void ); wint_t _getwche( void );
Return Value
Returns the character read. There is no error return.
Remarks
The _getch and _getwch functions read a single character from the console without echoing. _getche and _getwche read a single character from the console and echo the character read. None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
Generic-Text Routine Mappings
| TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
|---|---|---|---|
| _gettch | _getch | _getch | _getwch |
| _gettche | _getche | _getch | _getwche |
Requirements
| Routine | Required header | Compatibility |
|---|---|---|
| _getch | <conio.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _getche | <conio.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _getwch | <conio.h> or <wchar.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _getwche | <conio.h> or <wchar.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
// crt_getch.c
// compile with: /c
/* This program reads characters from
* the keyboard until it receives a 'Y' or 'y'.
*/
#include <conio.h>
#include <ctype.h>
int main( void )
{
int ch;
_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );
_putch( ch );
_putch( '\r' ); /* Carriage return */
_putch( '\n' ); /* Line feed */
}
Input
abcdey
Output
Type 'Y' when finished typing keys: Y
See Also
Console and Port I/O Routines | _cgets | getc | _ungetch | Run-Time Routines and .NET Framework Equivalents