What is the meaning of the expression main () returns an int and returns nothing to the body?

Duplicate

Why does int main () {} compile?

What is the point of the expression main () returns an int and returns nothing in the body?

-1


source to share


3 answers


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;"

.

+9


source


see this SO question for an answer.



Quick answer: because the standard says it's ok and is implied return 0;

.

+2


source


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.

0


source







All Articles