Machine-translated by Google. Questions are authored in English.
Top

Discussion

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 'a' */
        printf("%c", c);
        ungetc(c, stdin);
    }
    return 0;
}

 

  • A.aaaa
  • B.aaaaa
  • C.Garbage value.
  • D.Error in ungetc statement.

Answer: B

Correct answer: aaaaa

ungetc pushes the character back onto the input stream, so the next scanf reads the same character again. The loop therefore reads and prints the same a on all five passes, giving aaaaa. Only one character of pushback is guaranteed, which is all this needs.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.