0
Can anyone explain why output is 0000 of given c snippet?
int main() { static int i=5; if(--i) { main(); printf("%d",i); } }
1 Answer
0
make note that static var is inited ONLY one time and though you call main inside itself, the "i" var will have last value (differently by not-static vars) then:
- your call main() while "i" is equal to 0 following this calls:
main() i=4
main() i=3
main() i=2
main() i=1
main() i=0
after this it go back to main calls while to first main call BUT because "i" its static, it will have same value inside all main calls and because "i" is not modified anymore, "i" will be always 0 then all printf inside main calls will print SAME value of "i" 0 printing it 4 times (because at last main nothing will be printed)