_ungetch

Pushes back the last charcter read from the console.

int _ungetch( int c );

Routine Required Header Compatibility
_ungetch <conio.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

_ungetch returns the character c if it is successful. A return value of EOF indicates an error.

Parameter

c

Character to be pushed

Remarks

The _ungetch function pushes the character c back to the console, causing c to be the next character read by _getch or _getche. _ungetch fails if it is called more than once before the next read. The c argument may not be EOF.

Example

/* UNGETCH.C: In this program, a white-space delimited 
 * token is read from the keyboard. When the program 
 * encounters a delimiter, it uses _ungetch to replace 
 * the character in the keyboard buffer.
 */

#include <conio.h>
#include <ctype.h>
#include <stdio.h>

void main( void )
{
   char buffer[100];
   int count = 0;
   int ch;
   ch = _getche();
   while( isspace( ch ) )      /* Skip preceding white space. */
      ch = _getche();
   while( count < 99 )         /* Gather token. */
   {
      if( isspace( ch ) )      /* End of token. */
         break;
      buffer[count++] = (char)ch;
      ch = _getche();
   }
   _ungetch( ch );            /* Put back delimiter. */
   buffer[count] = '\0';      /* Null terminate the token. */
   printf( "\ntoken = %s\n", buffer );
}

Output

White
token = White

Console and Port I/O Routines

See Also   _cscanf, _getch