https://www.tutorialspoint.com/c_standard_library/c_function_ungetc.htm

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/3d5dd415-595f-4dea-88a5-7f47b660dfc3/c-mini-logo.jpg

The C Standard Library

Description

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.

Declaration

Following is the declaration for ungetc() function.

int ungetc(int char, FILE *stream)

Parameters

Return Value

If successful, it returns the character that was pushed back otherwise, EOF is returned and the stream remains unchanged.

Example

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 −