Syntax
#include <stdio.h> int ungetc(int c, FILE *stream);Description
ungetc pushes the byte c back onto the given input stream. However, only one sequential byte is guaranteed to be pushed back onto the input stream if you call ungetc consecutively. The stream must be open for reading. A subsequent read operation on the stream starts with c. The byte c cannot be the EOF character.
Bytes placed on the stream by ungetc will be erased if fseek, fsetpos, rewind, or fflush is called before the byte is read from the stream.
ungetc returns the integer argument c converted to an unsigned char, or EOF if c cannot be pushed back.
In this example, the while statement reads decimal digits from an input data stream by using arithmetic statements to compose the numeric values of the numbers as it reads them. When a nondigit character appears before the end of the file, ungetc replaces it in the input stream so that later input functions can process it.
#include <stdio.h> #include <ctype.h> int main(void) { int ch; unsigned int result = 0; while (EOF != (ch = getc(stdin)) && isdigit(ch)) result = result * 10 + ch -'0'; if (EOF != ch) /* Push back the nondigit character onto the input stream */ ungetc(ch, stdin); printf("Input number : %d\n", result); return 0; /**************************************************************************** For the following input: 12345s The output should be: Input number : 12345 ****************************************************************************/ }Related Information