English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C / C ++the ungetc()

This functionungetc()Get a character and push it back into the stream so that it can be read again.

Thisungetc()C Language Syntax

int ungetc(int character, FILE *stream)

Here,

character-the character to be pushed back into the stream.

stream-is a pointer to a file object.

Thisungetc()C Language Example

Example

#include <stdio.h>
int main() {
   int c;
   while ((c = getchar()) != '0')
   putchar(c);
   ungetc(c, stdin);
   c = getchar();
   putchar(c);
   puts("");
   printf("End!");
   return 0;
}

Output Result

s a b c t h 0
End!

In the above program, an int type character is declared. It will read characters until it encounters 0/It will display the character and print "The End!" when it encounters zero.

int c;
while ((c = getchar()) != '0')
putchar(c);
ungetc(c, stdin)
c = getchar();
putchar(c);
puts("");
printf("End!");