Determine Output:
void main()
{
static int i=5;
if(--i){
main();
printf("%d ", i);
}
}
void main()
{
static int i=5;
if(--i){
main();
printf("%d ", i);
}
}
A. 5 4 3 2 1
B. 0 0 0 0
C. Infinite Loop
D. None of These
Answer: Option B
i is initially 5.
main() is called recursively.
i is decremented to 4, and main() is called recursively.
i is decremented to 3, and main() is called recursively.
i is decremented to 2, and main() is called recursively.
i is decremented to 1, and main() is called recursively.
i is decremented to 0, and main() is called recursively.
i is printed (0).
Control goes back to the previous recursive call. i is printed (1).
Control goes back to the previous recursive call. i is printed (2).
Control goes back to the previous recursive call. i is printed (3).
Control goes back to the previous recursive call. i is printed (4).
Therefore, the output of the program will be 0000.
I think it will be 4 3 2 1
But printf statement is inside if loop, so when the value of variable i is 0, control comes out of the loop and there end the program. So none of these is the correct answer option D .
Sir. In this program the print function is never called. As every till the i becomes 0 it returns to main() with executing the print function. And when the i becomes 0 the loop condition fails and it breaks.
output will be:4 3 2 1
plz explain
souravkumarnanda@gmail.com