tags:

views:

290

answers:

4

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?

+2  A: 

see this SO question for the answer.

The quick answer is: because the standard says it's ok, and a return 0; is implied.

e.James
+7  A: 

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

paxdiablo
A: 

Here is another question on SO and answer: http://stackoverflow.com/questions/276807/return-0-implicit#276814

Johannes Schaub - litb
A: 

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.

Assaf Lavie