if (--i)
This will evaluate true the first time (--i == 4
). The code recurses into main()
. (Recursion: A function calling itself.)
As i
is static, it will retain its value of 4
(as opposed to an automatic variable, which would be initialized to 5
again). The if (--i)
in this second execution of main()
will again be true (evaluating to 3
), and will again call main()
(for a third execution of the function).
The same for --i == 2
and --i == 1
, for four executions of main()
(including the first, non-recursive one) total that are evaluating the if
condition to true.
The next recursion will evaluate the if
condition to --i == 0
, and thus false. Skipping the if
clause, the function call will just return. i
is zero at this point, and -- being static
, i.e. only one persistent i
for all instances of main()
-- will remain at that value.
The main()
call one level up the stack -- the one that evaluated --i == 1
, then called main()
and was waiting for it to return -- will now continue with the statement after the call to main()
, and printf()
the current value of i
... which is 0
.
The same happens three more times (for a total of four) until the top-most main()
returns. You get four times the current value of i
, which is 0
.
Note that calling main()
from your program is allowed in C, but not in C++. This is specifically for calling main()
recursively; for other functions, it is allowed in either language.