0

Why the output comes like that?

Give the reason for this output. int main() { static int i= 5; If (--i) { main(); printf("%d ", i); } } Output : 0 0 0 0

23rd Jul 2020, 2:29 PM
Ajay S
1 Antwort
+ 2
Since i is static, the same variable i is shared for each and every call to main. Keep that in mind when you trace the program. Trace it at a low level on your own. I'll make a few comments to explain it from a higher level. It is only when i is decremented to 0 that if (--i) evaluates to false and main() stops being called. After main() stops being called, the printf statements run. 0 is printed 4 times because main() is called 5 times total with the deepest call printing nothing. If you want to paste that code somewhere to run it, you'll need to replace "If" with "if" and add #include <stdio.h> at the top.
23rd Jul 2020, 3:08 PM
Josh Greig
Josh Greig - avatar