ungetc
raw OCR
Pushes a character back into input stream.
Defined in header <stdio.h>
Syntax
#include <stdio.h>
int ungetc(int c, FILE *stream);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ | ■ |
Remarks
ungetc pushes the character c back onto the named input stream, which must be open for reading. This character will be returned on the next call to getc or fread for that stream. One character can be pushed back in all situations. A second call to ungetc without a call to getc will force the previous character to be forgotten. A call to fflush, fseek, fsetpos, or rewind erases all memory of any pushed-back characters.
Return value
On success, ungetc returns the character pushed back; it returns EOF if the operation fails.
See also
Example
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int i=0;
char ch;
puts("Input an integer followed by a char: ");
/* read chars until nondigit or EOF */
while ((ch = getcharO) != EOF && isdigit(ch))
i = 10 * i + ch - 48; /* convert ASCII into int value */
/* if nondigit char was read, push it back into input buffer */
if (ch != EOF)
ungetc (ch, stdin);
printfC'i = %d, next char in buffer = %c\n", i, getcharO);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/stdio.h/ungetc/
· short form /3.1/ungetc/