Sail E0 Webinar
Question


What will be the output of the program?


#include<stdio.h>
int main()
{
int i;
char c;
for(i=1; i
Options:
A .  aaaa
B .  aaaaa
C .  Garbage value.
D .  Error in ungetc statement.
Answer: Option B

for(i=1; i<=5; i++) Here the for loop runs 5 times.

Loop 1
scanf("%c", &c); Here we give 'a' as input. 
printf("%c", c); prints the character 'a' which is given in the previous "scanf()" statement. 
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

Loop 2
Here the scanf("%c", &c); get the input from "stdin" because of "ungetc" function. 
printf("%c", c); Now variable c = 'a'. So it prints the character 'a'. 
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

This above process will be repeated in Loop 3Loop 4Loop 5.



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers