I found some interesting code lines:
#include <stdio.h>
int main()
{
printf("Hi there");
return main();
}
It compiles OK (VS2013) and ends up in stackoverflow error because of the recursive call to main()
. I didn't know that the return
statement accepts any parameter that can be evaluated to the expected return data type, in this example even int main()
.
Standard C or Microsoft-ish behaviour?
Well, a
return
statement can have an expression.Quoting
C11
standard, chapter 6.8.6.4, Thereturn
statement.so, in case of
return main();
, themain();
function call is the expression.And regarding the behaviour of
return main();
, this behaves like a normal recursive function, the exception being an infinite recursion in this case.As long as
C
standard is considered, it does not impose any restriction upon callingmain()
recursively.However, FWIW, AFAIK, in
C++
, it is not allowed.