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;
}
bbbb
bbbbb
b
Error in ungetc statement.
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 2 of 2.

Amudhan said:   1 decade ago
Shouldn't the ungetc() have a pointer to an input stream? stdout is an output stream?

Srn said:   1 decade ago
What is the logic behind program I couldn't understand. Please help me?

Swasthika said:   1 decade ago
I got the output as -> bbbbb which is option (B) and not option (C).

Abc said:   1 decade ago
Does it mean. . Each character entered will be freed by ungetc () ?

Mahesh said:   1 decade ago
I couldn't get the logic of this o/p can anyone help me?

Arti said:   9 years ago
Please. Can someone elaborate how the output is b?

Rita said:   7 years ago
Please explain the use of ungetc().

Nayak said:   1 decade ago
What is logic to implement it?


Post your comments here:

Your comments will be displayed after verification.