Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

ungetc - push byte back onto input stream

&pagelevel(4)&pagelevel

Syntax

#include <stdio.h>

int ungetc(int c, FILE *stream);

Description ungetc() converts the previously read byte c to type unsigned char and pushes it back

onto the input stream pointed to by stream. The pushed-back bytes will be returned by
subsequent reads on that stream in reverse order. A successful intervening call to a
filepositioning function (fseek(), fsetpos() or rewind()) for the same data stream will
delete any pushed-back bytes for the stream. The external storage associated with the
stream remains unchanged.

BS2000
A call to one of the following functions cancels the effects of the ungetc call (e.g. backward
positioning): fseek(), fsetpos(), lseek(), rewind(), fflush(). (End)

One byte of pushback is guaranteed. If ungetc() is called too many times on the same
stream without an intervening read or file-positioning operation on that stream, the
pushback operation may fail. A maximum of {BUFSIZE} bytes can be pushed back in the
C runtime system (see stdio.h).

If the value of c is equal to the macro EOF, the operation will fail and the input stream will
remain unchanged.

A successful call to ungetc() 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 bytes
will be the same as it was before the bytes were pushed back. The file-position indicator is
decremented by each successful call to ungetc(); if its value was 0 before a call, its value
will be indeterminate after the call.

Return val.

Byte pushed back

upon successful completion.

EOF

if c is equal to EOF or if an error occurs.

Notes

At least one byte must always have been read from the file before the first ungetc() call.

The program environment determines ungetc() is executed for a BS2000 or POSIX file.

BS2000
If a byte other than the one just read is pushed back onto the buffer when accessing
BS2000 files, the behavior will depend on whether KR or ANSI functionality is set:

  • KR functionality (only available with C/C++ versions lower than V3): when the buffer
    contents are written to the external file, the original data is not changed.

  • ANSI functionality: when the buffer contents are written to the external file, the original
    data is not changed, i.e. the original data prior to the ungetc() call is always written to
    the external file. (End)

See also

fseek(), getc(), fsetpos(), read(), rewind(), setbuf(), stdio.h.