Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


void main()
{
static int i = 5;
if(--i)
{
main();
printf("%d
",i);
}
}

what would be output of the above program and justify your
answer?

}

Answers were Sorted based on User's Feedback



void main() { static int i = 5; if(--i) { main(); printf("%d ",i); } } w..

Answer / samrat

Ans: 0 0 0 0

The first thing you have to remember is that static
variables are initialized only once. The second thing is
that static variables have a life time scope and they retain
their value between function calls.

"i" is first initialized to 5. in the if condition the value
of i is changed to 4. main() is called again and the value
of i is changed to 3 in the if condition and main is called
again. Now the value of i is changed to 2 and main is called
again. Now the value of i is changed to 1 and main is called
again. After this the value of i is changed to "0" and the
block is excited.

As the value of i is now "0", it is printed 4 times for each
of the calls for main(). So the ans will be
0
0
0
0

Thanks,
Samrat

Is This Answer Correct ?    65 Yes 4 No

void main() { static int i = 5; if(--i) { main(); printf("%d ",i); } } w..

Answer / srsabariselvan

0
0
0
0

static variable's value is stored in memory statically upto
end of the program. so if the variable comes out of the
function it retains its value

Is This Answer Correct ?    11 Yes 5 No

void main() { static int i = 5; if(--i) { main(); printf("%d ",i); } } w..

Answer / sri ram

This prog'll not produce any output since the value of i
reduces when it reaches zero if block will not be executed
and the program is terminated....

Is This Answer Correct ?    6 Yes 15 No

void main() { static int i = 5; if(--i) { main(); printf("%d ",i); } } w..

Answer / senthil kumar.s

ans:0

Is This Answer Correct ?    1 Yes 12 No

void main() { static int i = 5; if(--i) { main(); printf("%d ",i); } } w..

Answer / biren

we can't call a main function with in main.

Is This Answer Correct ?    7 Yes 31 No

Post New Answer



More C Interview Questions

4)What would be the output? main() { int num=425; pf("%d",pf("%d",num)); } a)Comp error b)4425 c)4253 d)3435 e)none

10 Answers  


What is volatile variable in c with example?

0 Answers  


Explain the process of converting a Tree into a Binary Tree.

0 Answers   Ignou,


What is declaration and definition in c?

0 Answers  


Will Macros support multiple arguments ?

7 Answers   Oracle,






nic scientist exam

1 Answers   NIC,


here is a link to download Let_Us_C_-_Yashwant_Kanetkar

3 Answers   Microsoft,


how to write hello word without using semicolon at the end?

6 Answers   Accenture,


Why array is used in c?

0 Answers  


how many key words availabel in c a) 28 b) 31 c) 32

0 Answers  


Place the #include statement must be written in the program?

0 Answers  


how can f be used for both float and double arguments in printf? Are not they different types?

0 Answers  






Categories