What is ungetc()?
The ungetc() function is part of the C standard library and is declared in the
Syntax of ungetc()
int ungetc(int ch, FILE *stream);
- ch → Character to be pushed back (converted to unsigned char)
- stream → Input stream (must be open for reading)
- Returns: On success, returns the character pushed back. On failure, returns EOF.
Key Characteristics of ungetc() Function
- You can only push back one character in some implementations. Pushing back multiple characters may not work reliably.
- ungetc() only works on input streams, not output streams.
- The pushed-back character will be the next character read by functions like getc() or fgetc().
- ungetc() does not change the file position indicator.
Example of ungetc() Function
Example 1:
#include <stdio.h> int main() { FILE *quizFile; int mcqChar; // Open file in read mode quizFile = fopen("mcqdata.txt", "r"); if (quizFile == NULL) { printf("Unable to open 'mcqdata.txt' for reading quiz data.\n"); return 1; } // Read first character from the file mcqChar = fgetc(quizFile); printf("First character read from MCQ file: %c\n", mcqChar); // Push it back to the stream if (ungetc(mcqChar, quizFile) == EOF) { printf("Failed to push character back into the stream.\n"); fclose(quizFile); return 1; } // Re-read the same character int quizChar = fgetc(quizFile); printf("Character read again after ungetc(): %c\n", quizChar); fclose(quizFile); return 0; }
Here’s the expected output for the ungetc() example, assuming mcqdata.txt contains the following content:
Sanfoundry MCQs are helpful!
Program Output
First character read from MCQ file: S Character read again after ungetc(): S
This C program shows how to use the ungetc() function to put a character back into a file stream. It opens a file called mcqdata.txt and reads the first character, which is ‘S’ in this case. Then, it uses ungetc() to push the character back. When it reads again, it gets the same character ‘S’. This proves that ungetc() works. The program is useful when you want to “peek” at a character but still need to read it again later.
Example 2:
#include <stdio.h> int main() { int mcqInput; printf("Sanfoundry Quiz - Enter a character: "); mcqInput = getchar(); // Push the character back into stdin ungetc(mcqInput, stdin); // Read the character again int reRead = getchar(); printf("Character re-read from input stream: %c\n", reRead); return 0; }
Sample Output
Sanfoundry Quiz - Enter a character: A Character re-read from input stream: A
This C program shows how ungetc() works with keyboard input. It first asks the user to enter a character using getchar(). Then, it pushes that character back into the input stream with ungetc(). After that, it reads the same character again using getchar() and prints it. This confirms that you can read the same character again after putting it back.
Limitations and Considerations
- Buffer Size: Only one character is guaranteed to be pushed back onto the stream. Attempting to push back multiple characters without intervening reads may fail.
- Binary vs. Text Streams: The behavior of ungetc() can differ between binary and text streams, especially concerning the file position indicator.
- Thread Safety: When working with multithreaded applications, ensure proper synchronization when using ungetc() to avoid race conditions.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check C Books
- Practice Computer Science MCQs
- Apply for C Internship