Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

_ungetch, _ungetwch

Pushes back the last character read from the console.

int _ungetch(
   int c 
);
wint_t _ungetwch(
   wint_t c 
);

Parameter

c
Character to be pushed.

Return Value

Both functions return the character c if successful. If there is an error, _ungetch returns a value of EOF and _ungetwch returns WEOF.

Remarks

These function push the character c back to the console, causing c to be the next character read by _getch or _getche (or _getwch or _getwche). _ungetch and _ungetwch fail if they are called more than once before the next read. The c argument may not be EOF (or WEOF).

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_ungettch _ungetch _ungetch _ungetwch

Requirements

Routine Required header Compatibility
_ungetch <conio.h> Win 98, Win Me, Win NT, Win 2000, Win XP
_ungetch <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_ungetch.c
// compile with: /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>

int 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 );
}

Input

White

Output

token = White

See Also

Console and Port I/O Routines | _cscanf | _getch | Run-Time Routines and .NET Framework Equivalents

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.