Duplicate
http://stackoverflow.com/questions/22239/why-does-int-main-compile
What is the significance of saying main() returns int and returning nothing in body?
http://stackoverflow.com/questions/22239/why-does-int-main-compile
What is the significance of saying main() returns int and returning nothing in body?
see this SO question for the answer.
The quick answer is: because the standard says it's ok, and a return 0;
is implied.
The C++ standard states:
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type
int
, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:
int main()
int main(int argc, char* argv[])
If control reaches the end of main without encountering a
return
statement, the effect is that of executing"return 0;"
.
Here is another question on SO and answer: http://stackoverflow.com/questions/276807/return-0-implicit#276814
I heard once that this peculiarity (that if you don't return a value in main, it implicitly means "return 0") is owing to one of the C++ authors saying, at the time, that he will simply not allow C++ to be a language in which the basic "Hello, World" program has to have an ugly "return 0" at the end of it.