...
The
ungetc
function pushes the character specified byc
(converted to anunsigned char
) back onto the input stream pointed to bystream
. Pushed-back characters will be returned by subsequent reads on that stream in the reverse order of their pushing. A successful intervening call (with the stream pointed to bystream
) to a file positioning function (fseek
,fsetpos
, orrewind
) discards any pushed-back characters for the stream. The external storage corresponding to the stream is unchanged.One character of pushback is guaranteed.
Consequently, subsequent multiple calls to ungetc()
on the same stream must be separated by a call to a read function or a file-positioning function (which will discard any data pushed by ungetc()
).
Likewise, for ungetwc()
C99 only guarantees one wide character of pushback (section 7.24.3.10). Consequently, multiple calls to ungetwc()
on the same stream must be separated by a call to a read function or a file-positioning function (which will discard any data pushed by ungetwc()
).
Non-Compliant Code Example
...
If used improperly, ungetc()
and ungetwc()
can cause data to be truncated or lost.
...