17

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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
kiewic
  • 15,852
  • 13
  • 78
  • 101
  • Are you sure you want this tagged "C" as well as "C++"? Your question seems to only pertain to C++. FWIW, C99 provides implicit return 0 in main, earlier standards of C do not. – Chris Young Nov 10 '08 at 06:32
  • 3
    This reminds me of the time I was given a 9/10 on a math assignment because I didn't write the date at the top right of the page and underline it. – Steve Jessop Nov 10 '08 at 12:48
  • 1
    Did you miss the `return 0` in `main` or in a different function? It is only implicit in `main`. – fredoverflow May 09 '10 at 15:33
  • @FredOverflow. I miss it in main function. – kiewic May 11 '10 at 03:31

2 Answers2

29

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 automatic 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.

You can learn more here.

ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 4
    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? – mmcdole Oct 14 '09 at 17:57
0

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
  • 10,195
  • 10
  • 55
  • 84
  • 1
    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 Nov 10 '08 at 12:56
  • 2
    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 Nov 10 '08 at 12:59