There are multiple problems in this code:
is not included, calling printf("%d", i);
has undefined behavior.
The prototype for main
without arguments is int main(void)
. Defining main
as void main()
is non portable and incorrect on some platforms. Always use the Standard prototypes for main
and return a valid exit status for good style.
It is bad style to call the main
function recursively in C, and it has undefined behavior in C++.
Ignoring the fact that undefined behavior means anything can happen, including Indian firms using horrendous programming tests, the reason the program prints 0000
is you probably mistyped the if (i--)
test or misplaced the braces from memory, or the guy is plain wrong. The main
function calls itself recursively 3 times decrementing the same i
variable each time until it becomes 0
and each instance prints the value of i
, namely 0
before it returns to its caller, including the initial invocation of main()
.
static int i = 4;
inside a function defines a global variable i
with an initial value of 4
that can only be accessed inside the block where it is defined. All recursive instances of main
access the same variable.