Name
ungetch - Pushes a character back to the console.
Syntax
#include <conio.h>
int ungetch(c)
int c;
Description
The ungetch function pushes the character c back to the
console, causing c to be the next character read by getch or
getche. The ungetch function fails if it is called more than
once before the next read. The c argument may not be EOF.
Return Value
The ungetch function returns the character c if it is
successful. A return value of EOF indicates an error.
See Also
cscanf(DOS), getch(DOS), getche(DOS)
Example
#include <conio.h> #include <ctype.h> #include <stdio.h>
char buffer[100]; int count = 0; int ch;
main()
{
ch = getche();
/* skip preceding white space */
while (isspace(ch))
ch = getche();
while (count < 99) /* Gather token */
{
if (isspace(ch)) /* End of token */
break;
buffer[count++] = ch;
ch = getche();
}
ungetch(ch); /* Put back delimiter */
/* Null terminate the token */
buffer[count] = '\0';
printf( "\ntoken = %s\n", buffer );
}
In this program, tokens are read from the keyboard delimited
by blanks and newline characters. When the program
encounters a delimiter, it uses ungetch to replace the
delimiter in the keyboard buffer.
(printed 6/18/89)