https://www.tutorialspoint.com/c_standard_library/c_function_ungetc.htm
The C Standard Library
The C library function *int ungetc(int char, FILE stream) pushes the character char (an unsigned char) onto the specified stream so that the this is available for the next read operation.
Following is the declaration for ungetc() function.
int ungetc(int char, FILE *stream)
If successful, it returns the character that was pushed back otherwise, EOF is returned and the stream remains unchanged.
The following example shows the usage of ungetc() function.
#include <stdio.h>int main () {
FILE *fp;
int c;
char buffer [256];
fp = fopen("file.txt", "r");
if( fp == NULL ) {
perror("Error in opening file");
return(-1);
}
while(!feof(fp)) {
c = getc (fp);
/* replace ! with + */
if( c == '!' ) {
ungetc ('+', fp);
} else {
ungetc(c, fp);
}
fgets(buffer, 255, fp);
fputs(buffer, stdout);
}
return(0);}
Let us assume, we have a text file file.txt, which contains the following data. This file will be used as an input for our example program −
this is tutorials point
!c standard library
!library functions and macros
Now, let us compile and run the above program that will produce the following result −