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; }
3 Answers
+ 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.
+ 1
HonFu Oussama Messaoudi thanks for the response
0
the output will be : 65432
why because itâs the logic of static keyword and recursivity