It is legal to recurse on main
in C* (although why would you?)
Additionally, C11 (5.1.2.2.1) states:
It shall be defined with a return type of int
... [or] If the return type is not compatible with int
, the
termination status returned to the host environment is unspecified.
(C++ states that the return type must be int
)
So this portion of the code is actually standard-compliant, but the actual return is implementation-defined (thanks @chux)
Can anyone explain why the printf executes or is that guy wrong?
If you're compiling C with a C++ compiler, guy is wrong. Otherwise the code will compile.
Now that this is out of the way, the printf
will indeed execute because the static
variable is only ever initialized once, as per 6.2.4/3
An object whose identifier is declared [...]with the
storage-class specifier static
has static storage duration. Its lifetime is the entire
execution of the program and its stored value is initialized only once, prior to program
startup
*It is definitely NOT legal in C++. From [basic.start.main]:
"The function main shall not be used within a program". There's a lot of C++ asides in this answer because the question was originally tagged as C++ and the information is useful