Topic: int main() can you show me the part of the standard for c++ that says I don't have to return a value from main?


Author: Steve Clamage <clamage@eng.sun.com>
Date: Mon, 4 Jun 2001 16:20:02 GMT
Raw View
On Sun, 3 Jun 2001, James Dennett wrote:
> >
> > Section 3.6.1.5 - 'If control reaches the end of main without encountering a
> > return statement, the effect is that of executing return 0;'
> >
> > But why? That's what I want to know.
>
> It does have the desirable effect of removing one commonly
> invoked piece of undefined behaviour from the Standard; it
> has been very common for sloppy users to fail to return a
> value from main.  (It has the undesirable effect of
> introducing an inconsistency, in that main is the only
> function to which this applies.)
>
> I don't know if that was the reason for the change though,
> as I was not part of the Standardisation process when the
> rule was written.

It was (and still is) common for implementations not to need a
return statement in main, even in C programs. "Falling off the end"
of main has the effect of returning a status of success. Some other
systems do not use the return value at all, and the returned value
is not meaningful.

C++ originally required a return statement in main. There were many
complaints from users who had to add return statements to satisfy
compilers, even though the system never needed the return statement.

Although I favored this language change when it was proposed (I
think in 1992), I have since thought that it might better have
been left as a quality-of-implementation issue. As with other
unportable features, a compiler could optionally warn about a
missing return statement, rejecting the code in "pedantic" mode.

Of course, a compiler is currently allowed to warn about a missing
return statement (because a compiler can warn about anything at all.)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]