C Function Ungetc
# C Library Function - ungetc()
[ C Standard Library - ](#)
## Description
The C library function **int ungetc(int char, FILE *stream)** pushes the character **char** (an unsigned character) back into the specified stream **stream**, so that it is the next character to be read.
## Declaration
Here is the declaration for the ungetc() function.
```c
int ungetc(int char, FILE *stream)
## Parameters
* **char** -- This is the character to be pushed back. The character is passed as its corresponding int value.
* **stream** -- This is a pointer to a FILE object that identifies the input stream.
## Return Value
If successful, it returns the character pushed back, otherwise it returns EOF, and the stream remains unchanged.
## Example
The following example demonstrates the usage of the ungetc() function.
## Example
```c
#include
int main ()
{
FILE *fp;
int c;
char buffer ;
fp = fopen("file.txt","r");
if( fp == NULL )
{
perror("Error 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 has the following content. This file will be used as an input for the example:
this is !c standard library !library functions and macros
Let us compile and run the above program, this will produce the following result:
this is +c standard library +library functions and macros
[ C Standard Library - ](#)
[](#)[C Standard Library β ](#)
[C Standard Library β ](#)[](#)
YouTip