Hacker News new | ask | show | jobs
by Tomte 4198 days ago
Not return-ing but just falling through to the closing } is perfectly fine and defined as returning 0.
2 comments

To clarify: this is only for main(), in C++: (see c3.6.1p5 of the standard)

"A return statement in main has the effect of leaving the main function (destroying any objects with auto- matic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0"

It's really best not to get in the habit of not returning from a function though, as in the vast majority of cases, you'll get a random value (well, whatever's in the appropriate register) as the return result, and unless you specifically configure it, most compilers won't even warn you!

Thank you for pointing this out! It is even described in the language's specification:

"If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; 11) reaching the } that terminates the main function returns a value of 0." ( http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf, 5.1.2.2.3 Program termination )