tags:

views:

511

answers:

3
+5  Q: 

return 0 implicit

The last week on the ACM ICPC Mexico competition, I missed a "return 0" on a C++ program. For this reason we got punished with 20 minutes.

I had read that the standard does not oblige us to write it at the end of a main function. It is implicit, isn't it? How can I prove it?

We were using a Fedora system with a G++ compiler.

+10  A: 

You refer to the C++ Standard, chapter 3.6.1 paragraph 5:

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;

If you haven't got the Standard at hand, you can show then the paragraph in a Working Draft. Here is one for c++98, which already had this defined.

http://www.kuzbass.ru:8086/docs/isocpp/basic.html#basic.start.main

Johannes Schaub - litb
The judging is automated at the ACM competitions. So for some reason it didn't compile because of the lack of return statement. Could this of been a result of an old compiler?
Simucal
A: 

You could show them the line in Bjarne Stroustrup's book defining the standard where it states it: since it is the canonical standard for the language, it is not open for debate. Unfortunately I don't have a copy to look it up myself.

Stuart P. Bentley
Um. That book doesn't define the standard. The standard is BS ISO/IEC 14882:1998 plus the technical corrigenda. If the two disagree then Bjarne's book is wrong until the full standards body decides otherwise (of course he's on that body himself, so disagreements tend to be typos...)
Steve Jessop
I agree with the actual answer, by the way - I'd expect that book to be accepted as accurate in a matter like this by anyone who knows what they're doing. It describes the standard accurately afaik. It just doesn't "define" the standard, so it is in theory open to debate.
Steve Jessop
A: 

I once kept getting a wrong answer on an Online Judge because of that too, so I always remembered to add it even before coding the problem.

Ahmad Farid
Oh yes, I also learned the lesson :P
Kiewic