The ungetc() function takes a single character and shoves it back onto an input stream. It is the opposite of the getc() function, which reads a single character from an input stream. Also, ungetc() is an input function, not an output function.
int ungetc(int char, FILE *stream)
- char: specifies the int promotion of the character to be put back. The value is internally converted to an unsigned char when put back.
- stream: specifies the pointer to a FILE object that identifies an input stream.
Return Value: The function returns two kind of values.
- On success, the ungetc() function returns the character ch.
- On failure, EOF is returned without changing the stream.
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.
- A successful intervening call (with the stream pointed to by stream) to a file-positioning function ( fseek(), fsetpos(), or rewind()) discards any pushed-back bytes for the stream.
- The external storage corresponding to the stream shall be 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 shall 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 is unspecified after the call.
Below programs illustrate the above function.
Let us assume, we have a text file use1.txt, which contains the following data. This file will be used as an input for our example program, then the input and output are shown below:
Input: !c standard library !library function stdio.h-ungetc() Output: +c standard library +library function stdio.h-ungetc()
- Intel Graphics Software Engineer Interview Experience
- OpenMP | Introduction with Installation Guide
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Maximum absolute difference in an array
- Count the pairs of vowels in the given string
- X-Macros in C
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Print path from root to all nodes in a Complete Binary Tree
- Count the number of 1's and 0's in a binary array using STL in C++ ?
- Find elements of an Array which are Odd and Even using STL in C++
- Find elements of an array which are divisible by N using STL in C++
- All reverse permutations of an array using STL in C++
- Generics in C++
- Find the longest subsequence of an array having LCM at most K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.