C Programming - Library Functions - Discussion
Discussion Forum : Library Functions - Find Output of Program (Q.No. 4)
4.
What will be the output of the program?
#include<stdio.h>
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;
}
Answer: Option
Explanation:
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.
Discussion:
18 comments Page 1 of 2.
Rutu said:
5 years ago
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.
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.
Rita said:
7 years ago
Please explain the use of ungetc().
Ariyan Khna said:
9 years ago
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.
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.
Ghanshyam said:
9 years ago
I run the program then give output is-ppppp.
But how it will possible, where the answer is b.
But how it will possible, where the answer is b.
Arti said:
9 years ago
Please. Can someone elaborate how the output is b?
Type said:
9 years ago
What is the use of ungetc function in this code as the output is bbbbb which is same as printf?
Any explanation please?
Any explanation please?
Swasthika said:
10 years ago
I got the output as -> bbbbb which is option (B) and not option (C).
Pooja said:
1 decade ago
#include<stdio.h>
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.
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.
SHARMA said:
1 decade ago
I don't get above logic. Even whenever I'm running this program that time an error msg comes that is core dumped.
Cherishy said:
1 decade ago
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...).
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...).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers