Integer type (typically 4 bytes, -2,147,483,648 to 2,147,483,647)
1st Parameter
The character to be pushed back into the input stream. It is passed as an int. Must not be EOF (operation fails); Converted to unsigned char before push-back; Value outside 0-255 range still works (only last byte used);
2nd Parameter Type : FILE *
Pointer to FILE structure for file operations
2nd Parameter
Pointer to the input stream where the character will be pushed back. Must be open for reading; Must be valid (not NULL); Must have room in push-back buffer; Must not be at invalid stream state;
Read more about parameters of ungetc in parameters section
The ungetcfunction in C language pushes a character back onto a stream, where it is available for subsequent read operations.
ungetc is used for character stream manipulation when you need to "peek" at input without consuming it, making it essential for parsing and lexical analysis tasks. What makes it special is its ability to put a character back into the input stream, effectively "un-reading" it, which is particularly useful when you've read a character but realize it belongs to the next token or operation. The function works closely with input functions like fgetc, getc, and fscanf which will read the pushed-back character on their next call, and it's affected by file positioning functions like fseek and rewind which clear its push-back buffer.
To Summarize the Workflow:
1.
Takes character as int and FILE pointer as parameters
2.
Pushes back one character into input stream
3.
Decrements stream position indicator
4.
Returns pushed-back character on success
5.
Returns EOF if push-back fails
6.
Character becomes available for next read operation
Read more about return type and value ofungetc function in return section.
The ungetcfunction takes 2
parameters:
•
int `c`: The character to be pushed back into the input stream. It is passed as an int. Must not be EOF (operation fails); Converted to unsigned char before push-back; Value outside 0-255 range still works (only last byte used);
•
FILE * `stream`: Pointer to the input stream where the character will be pushed back. Must be open for reading; Must be valid (not NULL); Must have room in push-back buffer; Must not be at invalid stream state;
Important limitations: ● Only one character push-back guaranteed by standard ● Can't push back after EOF is reached ● Push-back buffer cleared by file positioning functions ● Attempting multiple push-backs may fail on some systems
The ungetc function return value :
Success: returns character that was pushed back (as int) Failure: returns EOF (-1) when: Input is EOF; Stream is invalid; Push-back buffer is full; Stream is in error state
Important Considerations: Return value matches input parameter on success; Can be safely compared with EOF; Doesn't indicate how many push-backs remain possible; Success doesn't guarantee another push-back will work;
Output
This example reads a character from a file, pushes it back, and then reads it again.