_ungetch

Description

Pushes back the last character read from the console.

#include <conio.h> Required only for function declarations  

int _ungetch( int c );

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. 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.

Compatibility

Standards:None

16-Bit:DOS

32-Bit:DOS32X

See Also

_cscanf, _getch, _getche

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++] = ch;

ch = _getche();

}

_ungetch( ch ); /* Put back delimiter. */

buffer[count] = '\0'; /* Null terminate the token. */

printf( “\ntoken = %s\n”, buffer );

}

Output

White

token = White