ungetc, ungetwc
Pushes a character back onto the stream.
int ungetc( int c, FILE *stream ); wint_t ungetwc( wint_t c, FILE *stream );
Parameters
- c
- Character to be pushed.
- stream
- Pointer to FILE structure.
Return Value
If successful, each of these functions returns the character argument c. If c cannot be pushed back or if no character has been read, the input stream is unchanged and ungetc returns EOF; ungetwc returns WEOF.
Remarks
The ungetc function pushes the character c back onto stream and clears the end-of-file indicator. The stream must be open for reading. A subsequent read operation on stream starts with c. An attempt to push EOF onto the stream using ungetc is ignored.
Characters placed on the stream by ungetc may be erased if fflush, fseek, fsetpos, or rewind is called before the character is read from the stream. The file-position indicator will have the value it had before the characters were pushed back. The external storage corresponding to the stream is unchanged. On a successful ungetc call against a text stream, the file-position indicator is unspecified until all the pushed-back characters are read or discarded. On each successful ungetc call against a binary stream, the file-position indicator is decremented; if its value was 0 before a call, the value is undefined after the call.
Results are unpredictable if ungetc is called twice without a read or file-positioning operation between the two calls. After a call to fscanf, a call to ungetc may fail unless another read operation (such as getc) has been performed. This is because fscanf itself calls ungetc.
ungetwc is a wide-character version of ungetc. However, on each successful ungetwc call against a text or binary stream, the value of the file-position indicator is unspecified until all pushed-back characters are read or discarded.
Generic-Text Routine Mappings
| TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
|---|---|---|---|
| _ungettc | ungetc | ungetc | ungetwc |
Requirements
| Routine | Required header | Compatibility |
|---|---|---|
| ungetc | <stdio.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| ungetwc | <stdio.h> or <wchar.h> | ANSI, 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_ungetc.c
/* This program first converts a character
* representation of an unsigned integer to an integer. If
* the program encounters a character that is not a digit,
* the program uses ungetc to replace it in the stream.
*/
#include <stdio.h>
#include <ctype.h>
int main( void )
{
int ch;
int result = 0;
/* Read in and convert number: */
while( ((ch = getchar()) != EOF) && isdigit( ch ) )
result = result * 10 + ch - '0'; /* Use digit. */
if( ch != EOF )
ungetc( ch, stdin ); /* Put nondigit back. */
printf( "Number = %d\nNext character in stream = '%c'",
result, getchar() );
}
Input
521a
Output
Number = 521 Next character in stream = 'a'
See Also
Stream I/O Routines | getc | putc | Run-Time Routines and .NET Framework Equivalents