What is the meaning of the expression main () returns an int and returns nothing to the body?
The C ++ standard says:
The implementation should not predetermine the main function. This function should not be overloaded. It must have a return type
int
, but otherwise its type is implementation-defined. All implementations must allow both of the following main definitions:
int main()
int main(int argc, char* argv[])
If the control reaches the end of main but does not encounter a statement
return
, the effect is to execute"return 0;"
.
source to share
see this SO question for an answer.
Quick answer: because the standard says it's ok and is implied return 0;
.
source to share
I heard this feature once (that if you don't return a value in main, it implicitly means "return 0") is due to the fact that one of the C ++ authors says that at that time he simply did not allow C ++ - it is a language in which the base "Hello, World" program must have an ugly "return 0" at the end.
source to share