ungetc -- push character back into input stream


cc ... -lc

#include <stdio.h>

int ungetc (c, stream) int c; FILE *stream;


The ungetc function pushes the character specified by c (converted to an unsigned char) back onto the input stream pointed to by stream. The pushed-back characters are returned by subsequent reads on that stream in the reverse order of their pushing. A successful intervening call (with the stream pointed to by stream) to a file-positioning function ( fseek(S) or rewind(S)) discards any pushed-back characters for the stream. The external storage corresponding to the stream is unchanged.

One character of pushback is guaranteed. If the ungetc function is called too many times on the same stream without an intervening read or file-positioning operation on that stream, the operation may fail.

If the value of c equals that of the macro EOF, the operation fails and the input stream is unchanged.

A successful call to the ungetc function clears the end-of-file indicator for the stream. The value of the file-position indicator for the stream after reading or discarding all pushed-back characters is the same as it was before the characters were pushed back. One character of pushback is guaranteed, provided something has already been read from the stream and the stream is actually buffered. In the case that stream is stdin, and the stream is buffered, one character may be pushed back onto the buffer without a previous read statement. For a stream, the value of its file-position indicator after a successful call to the ungetc function is unspecified until all pushed-back characters are read or discarded.


ungetc returns EOF if it cannot insert the character.


When stream is stdin, one character may be pushed back onto the buffer without a previous read statement.

See also

fseek(S), getc(S), setbuf(S), stdio(S)

Standards conformance

ungetc is conformant with:

X/Open Portability Guide, Issue 3, 1989 ;
ANSI X3.159-1989 Programming Language -- C ;
Intel386 Binary Compatibility Specification, Edition 2 (iBCSe2) ;
IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1) ;
and NIST FIPS 151-1 .

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003