Syntax
#include <conio.h> int _ungetch(int c);Description
If successful, _ungetch returns the character c. A return value of EOF indicates an error.
This example uses _getch to read a string delimited by the character 'x'. It then calls _ungetch to return the delimiter to the keyboard buffer. Other input routines can then process the delimiter.
#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