Syntax
#include <conio.h> int _getch(void); int _getche(void);Description
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.
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("Type 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("\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'.
****************************************************************************/
}
Related Information