0

Can anyone explain how this code is printing 5 ones?

#include <stdio.h> int main() { static int i = 6; if(--i){ main(); printf("%d", i+1); } return 0; }

3rd Feb 2020, 9:36 AM
Arshia
Arshia - avatar
3 odpowiedzi
+ 3
If i means: if i is something else than zero. If that happens, main is called recursively, while i becomes one lower. As soon as i, which is a static variable being the same in all these scopes, becomes zero, the if part isn't executed, so main (the deepest recursive call) returns. Then, for all the mains, that have been opened upon each other, i (0) + 1 us printed, until the outermost actual main function ends.
3rd Feb 2020, 10:02 AM
HonFu
HonFu - avatar
+ 1
HonFu Oussama Messaoudi thanks for the response
3rd Feb 2020, 4:49 PM
Arshia
Arshia - avatar
0
the output will be : 65432 why because it’s the logic of static keyword and recursivity
3rd Feb 2020, 10:02 AM
Oussama Messaoudi
Oussama Messaoudi - avatar