Why We Use Ungetc in C?

Important points about the function:
  • The ungetc() function pushes the byte specified by char (converted to an unsigned char) back onto the input stream pointed to by stream.
  • The pushed-back bytes is returned by subsequent reads on that stream in the reverse order of their pushing.

What is Ungetc used for?

The ungetc function pushes the character c back onto stream and clears the end-of-file indicator. The stream must be open for reading. A subsequent read operation on stream starts with c. An attempt to push EOF onto the stream using ungetc is ignored.

What is Ungetc in c programming?

ungetc() — Push Character onto Input Stream

The ungetc() function pushes the unsigned character c back onto the given input stream. However, only one consecutive character is guaranteed to be pushed back onto the input stream if you call ungetc() consecutively. The stream must be open for reading.

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.