C Programming - Library Functions - Discussion


What will be the output of the program?


int main()
    int i;
    char c;
    for(i=1; i<=5; i++)
        scanf("%c", &c); /* given input is 'b' */
        ungetc(c, stdout);
        printf("%c", c);
        ungetc(c, stdin);
    return 0;

[A]. bbbb
[B]. bbbbb
[C]. b
[D]. Error in ungetc statement.

Answer: Option C


The ungetc() function pushes the character c back onto the named input stream, which must be open for reading.

This character will be returned on the next call to getc or fread for that stream.

One character can be pushed back in all situations.

A second call to ungetc without a call to getc will force the previous character to be forgotten.

Rajesh said: (Aug 19, 2010)  
I couldn't get the logic of this o/p can anyone help me?

What does ungetc(c,stdout) statement will do?

Kalidass said: (Mar 3, 2011)  
Usually ungetc() fuctions used get a single character input,But here whenever we call the ungetc(c,stdout) it wont previous take char(i.e.,it releases from buffer or forgot the previous char)

Mahesh said: (Apr 12, 2011)  
I couldn't get the logic of this o/p can anyone help me?

Divya said: (Jul 7, 2011)  
Hai kalidass it means the ungetc (c, stdout) function won't display the previous char.

Nayak said: (Nov 3, 2011)  
What is logic to implement it?

Abc said: (May 6, 2012)  
Does it mean. . Each character entered will be freed by ungetc () ?

Srn said: (Sep 21, 2012)  
What is the logic behind program I couldn't understand. Please help me?

Amudhan said: (Jul 22, 2013)  
Shouldn't the ungetc() have a pointer to an input stream? stdout is an output stream?

Cherishy said: (Dec 8, 2013)  
scanf function reads the char c from i/p stream stdin (which is the input b), ungetc(c,stdout)(get unsigned char) pushes c to stdout(the monitor), printf also pushes the char c to output screen.

ungetc(c,stdin) pushes back the char to input stream stdin...this c is read again when scanf is implemented in next loop.

Notice that ungetc can b used only for 1 byte of data...hence first use of ungetc is forgotten and the o/p is only b (as a result of printf...).

Sharma said: (May 7, 2014)  
I don't get above logic. Even whenever I'm running this program that time an error msg comes that is core dumped.

Pooja said: (Aug 31, 2014)  

int main()
int i;
char c;
for(i=1; i<=5; i++)
scanf("%c", &c); /* given input is 'b' */
ungetc(c, stdout);

return 0;

If I run this code i got answer b. Means ungetc function work as printf.

But my problem is if ungetc send their input to monitor then it should be printed and then next b should be printed because of printf.

Although the answer is correct but i am not able to get the logic.

Swasthika said: (Jul 17, 2015)  
I got the output as -> bbbbb which is option (B) and not option (C).

Type said: (Jan 14, 2016)  
What is the use of ungetc function in this code as the output is bbbbb which is same as printf?

Any explanation please?

Arti said: (Jun 18, 2016)  
Please. Can someone elaborate how the output is b?

Ghanshyam said: (Aug 7, 2016)  
I run the program then give output is-ppppp.

But how it will possible, where the answer is b.

Ariyan Khna said: (Nov 12, 2016)  
ungetc(c, stdout);

Statement is invalid statement.
Program will be crashed by this message:
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

And the output will be : bbbbb.
If we remove ungetc(c, stdout); statement.

Rita said: (Dec 12, 2018)  
Please explain the use of ungetc().

Rutu said: (Jun 12, 2020)  
ungetc() pushes a character back into input stream.

That is it enable us to reenter the char as it pushes b into input stream again and again if you comment ungetc(c,stdin) you will able to enter input 5 times.
Also a second call to ungetc without getc will force the previous character to be forgotten so here
When you use ungetc(c, stdout) first it will get printed and pushes the cha back so printf also not print it and for next time we call it without calling getc so it makes it to forgotten the prev char hence it is only one time prints.

If you comment ungetc(c, stdout) you will get b 5 times.
And if you comment both ungetc it will take one char and print it.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.