Topic: Omitting return type of main.
Author: Boris Schaefer <sbo@psy.med.uni-muenchen.de>
Date: 1998/09/28 Raw View
Joe Keane <jgk@jgk.org> writes:
| And no one answered my question: how do i declare a function so that it
| returns zero when execution falls off the end? Please tell me.
You can't. The only exception is `main' and it must be declared `int'.
--
Boris Schaefer -- sbo@psy.med.uni-muenchen.de
Old programmers never die, they just branch to a new address.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW@my-dejanews.com
Date: 1998/09/17 Raw View
In article <35FA9C2F.446B@wizard.net>,
James Kuyper <kuyper@wizard.net> wrote:
> AllanW@my-dejanews.com wrote:
> ...
> > Function main() *must* specify a return type of int. There are a
> > lot of compilers that allow you to declare main as a void function,
> > but these compilers are wrong. Someday they will be fixed, too, so
> > just don't do it.
>
> You're an optimist on that issue, I fear.
Thank you! I certainly hope so!
All it takes for a non-compliant compiler to become compliant (on this
one issue) is to issue a warning for main that doesn't return int, and
then only when compiled in "full ISO/ANSI compliance" mode. It doesn't
have to reject the code, so programmers needn't fear invalidating all
of their legacy code and vendors needn't fear alienating their
customers. All it taks is one warning message, and everyone's happy.
ONE warning message! Probably one IF statement in the compiler, plus
one more entry in the "Warning string table" and two more pages in the
user manual. (One page to document the warning, one page for the
"revision history").
ONE LITTLE WARNING! For the big names (i.e. Microsoft, etc.) I give
it 18 months.
--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----=3D=3D Posted via Deja News, The Leader in Internet Discussion =3D=3D=
-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/09/17 Raw View
JMarshall@enterprise.net (John Marshall) writes:
>stephen.clamage@sun.com (Steve Clamage) wrote:
>> This feature of the C++ standard simply recognizes what has
>> been common practice in compilers and programs since before
>> C++ began.
>I just read another posting in this newsgroup, also from Steve, in which
>he said "to be adopted, a feature should solve a general programming
>problem of wide applicability for which there was no good workaround
>already". Adding "return 0;" just once no matter the size of the program
>sounds like a pretty good workaround to me.
It wasn't a new feature. It was existing practice. In 20 years of
C and C++ programming, I can remember using only one C or C++ compiler
that actually required a "return 0;" for the program to work correctly.
(Sometimes a compiler would reject a program with a missing return
to conform to the standard, not because it was necessary.)
>> It imposes no hardship on any programmer nor on
>> any implementor, and does not invalidate any existing code.
>Well, it makes this programmer feel ill. Would anyone care to explain
>how to implement it without the moral equivalent of
> if (current_function == "main")
> tack on a return statment
That's pretty much how its done in those cases where the return
is needed. Often it isn't, as I mentioned above. A complete program
usually involves some start and finish code supplied by the runtime
system, which can take care of that automatically.
OTOH, main is special in many ways, and the compiler has to have
a moderate amount of special-case code to deal with it. For example,
calling a function called "main" or taking its address is valid
only if it is not an external function in the global namespace.
Declaring a function called main with a return type other than int
also is valid only if it is not an external function in the global
namespace.
--
Steve Clamage, stephen.clamage@sun.com
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Joe Keane <jgk@jgk.org>
Date: 1998/09/17 Raw View
I think it's bizarre to say it's a `compiler bug' because the return
value of a function is indeterminate when execution falls off the end.
So far as i know, this has always been the case in C, for any function.
What's the problem, maybe the function doesn't return, maybe the return
value is not used for some reason, or maybe the programmer screwed up.
Well maybe the programmer did screw up, what about it?
Like i said, i ran across several programs that returned exit code 10,
or whatever. There didn't seem to be any disagreement about this: the
program is broken, it has a bug; add "return 0;" at the end of `main',
and the bug is fixed.
What next, is it a `compiler bug' if a variable that is not initialized
is not initialized?? Better to set it to zero, just in case?
If some compiler starts inserting code for me, because it thinks i'm a
moron, i think i'll tell the vendor where to stick their compiler.
Perhaps this is a difference in language philosophy: C people think the
compiler should take what the programmer writes and make machine code,
while C++ people think the programmer is an idiot and it's best to have
the compiler do lots of stuff behind their back, just in case.
The latter philosophy is of course doomed; if someone can't remember to
return a value from a function, how do you expect them to get the logic
right for some complicated algorithm? The idea that your average guy
off the street will write reliable C++ code is especially hilarious.
And no one answered my question: how do i declare a function so that it
returns zero when execution falls off the end? Please tell me.
--
Joe Keane, amateur mathematician
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/09/15 Raw View
John Marshall wrote:
> (Thinks some more...) Perhaps we could preload the symbol table with
> extern "bloody stupid calling convention" int main(...);
> so the condition becomes slightly less distasteful:
> if (current_function_convention == CONVENTION_BLOODY_STUPID)
> Okay, I feel a little better now, but it still smells like pandering
> to idiots.
Hear, hear! Falling off the end of main is just plain ugly.
--
Ciao,
Paul
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW@my-dejanews.com
Date: 1998/09/12 Raw View
In article <6t99n1$t80$1@cscnews.csc.calpoly.edu>,
dstubbs@falcon.csc.calpoly.edu (Daniel Stubbs) wrote:
> It is my understanding that in standard C++ it is not permitted
> to omit a function's return type. In particular, an omitted
> return type does not default to int.
Correct.
> Now main is an unusual function in that if a return is not
> encountered then, by default, a 0 is returned (I believe).
Correct.
> Is main an exception to the first paragraph above? I understand
> it is not--but I would like to be sure.
No. Main is an exception to a completely different rule.
You may be confusing "return type" with "return statement."
long somefunc() // The "return type" is long
{
return 0; // This is a "return statement"
// Since the expression is of type int, it will be promoted
// to the return type (long)
}
Function main() *must* specify a return type of int. There are a
lot of compilers that allow you to declare main as a void function,
but these compilers are wrong. Someday they will be fixed, too, so
just don't do it.
There is a different rule, which says that all functions must
exit by executing a return statement with a value. (Ignoring
fringe cases like exit and abort). There are only two other
exceptions to this rule. The first exception is void functions,
which may not execute a return statement with a value, but may
execute a return statement without a value. The other exception
is function main().
Ironically, the one function that's required to specify a return
type of int, is the only int function that's allowed to not bother.
--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1998/09/12 Raw View
dstubbs@falcon.csc.calpoly.edu (Daniel Stubbs) writes:
> It is my understanding that in standard C++ it is not permitted
> to omit a function's return type. In particular, an omitted
> return type does not default to int.
>
> Now main is an unusual function in that if a return is not
> encountered then, by default, a 0 is returned (I believe).
>
> Is main an exception to the first paragraph above? I understand
> it is not--but I would like to be sure.
No, main has a return type of int. You can fall off the end of main
without a return statement, in which case (int)0 is returned - it
still has int as its return type.
You also have to declare the parameters of main, if you think main
should have parameters. You have the freedom to define main in a
number of ways; it depends on your implementation which ones are
accepted. A declaration of main must be well-formed, of course.
Regards,
Martin
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/09/12 Raw View
Paul Black wrote:
>
> main() always has a return type of int. A main function with no
> return statement returns 0 if it drops off the end.
Does anyone know why they did that? Why are you allowed to fall off the
end of main? Is it to remain compatible with some old C crud that does
that? If so, why not just fix the code by adding "return 0;" at the end?
After all, it doesn't matter if your application is ten million lines of
code: there is still only one "main()", and therefore only one line of
code to add if the compiler complains about this particular anachronism.
--
Ciao,
Paul
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/09/12 Raw View
AllanW@my-dejanews.com wrote:
...
> Function main() *must* specify a return type of int. There are a
> lot of compilers that allow you to declare main as a void function,
> but these compilers are wrong. Someday they will be fixed, too, so
> just don't do it.
You're an optimist on that issue, I fear.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/09/12 Raw View
"Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
>Paul Black wrote:
>>
>> main() always has a return type of int. A main function with no
>> return statement returns 0 if it drops off the end.
>Does anyone know why they did that? Why are you allowed to fall off the
>end of main?
Part of the purpose of a standard is to codify existing practice.
Existing practice for many C and C++ compilers has been not to
require a return statement in main, and to provide the effect
of "return 0;" if a program falls off the end of main.
Al Stevens pointed out that some compilers didn't insist on
a return statement, but also didn't do anything predictable
if you omitted the return. That's just a compiler bug, not a
language feature.
This feature of the C++ standard simply recognizes what has
been common practice in compilers and programs since before
C++ began. It imposes no hardship on any programmer nor on
any implementor, and does not invalidate any existing code.
As an accidental side effect, the new rule removes the last
possible complaint about the lack of "void main". If you wanted
to write
void main() { ... }
you change "void" to "int", and you still don't have to add a
return. And your program is shorter by one character. :-)
--
Steve Clamage, stephen.clamage@sun.com
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: JMarshall@enterprise.net (John Marshall)
Date: 1998/09/14 Raw View
stephen.clamage@sun.com (Steve Clamage) wrote:
> This feature of the C++ standard simply recognizes what has
> been common practice in compilers and programs since before
> C++ began.
I just read another posting in this newsgroup, also from Steve, in which
he said "to be adopted, a feature should solve a general programming
problem of wide applicability for which there was no good workaround
already". Adding "return 0;" just once no matter the size of the program
sounds like a pretty good workaround to me.
> It imposes no hardship on any programmer nor on
> any implementor, and does not invalidate any existing code.
Well, it makes this programmer feel ill. Would anyone care to explain
how to implement it without the moral equivalent of
if (current_function == "main")
tack on a return statment
(Thinks some more...) Perhaps we could preload the symbol table with
extern "bloody stupid calling convention" int main(...);
so the condition becomes slightly less distasteful:
if (current_function_convention == CONVENTION_BLOODY_STUPID)
Okay, I feel a little better now, but it still smells like pandering
to idiots.
John
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: source@netcom.com (David Harmon)
Date: 1998/09/14 Raw View
On 12 Sep 1998 23:58:30 GMT, stephen.clamage@sun.com (Steve Clamage)
wrote:
>
>Al Stevens pointed out that some compilers didn't insist on
>a return statement, but also didn't do anything predictable
>if you omitted the return. That's just a compiler bug, not a
>language feature.
Really? It seemed to me to be, not a bug, but based directly on the C
standard wherein it is stated:
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. If the main function executes a return
that specifies no value, the termination status returned to the host
environment is undefined.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: dstubbs@falcon.csc.calpoly.edu (Daniel Stubbs)
Date: 1998/09/10 Raw View
It is my understanding that in standard C++ it is not permitted
to omit a function's return type. In particular, an omitted
return type does not default to int.
Now main is an unusual function in that if a return is not
encountered then, by default, a 0 is returned (I believe).
Is main an exception to the first paragraph above? I understand
it is not--but I would like to be sure.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Paul Black <paul@darwin.demon.co.uk>
Date: 1998/09/11 Raw View
dstubbs@falcon.csc.calpoly.edu (Daniel Stubbs) wrote:
> It is my understanding that in standard C++ it is not permitted
> to omit a function's return type. In particular, an omitted
> return type does not default to int.
> Now main is an unusual function in that if a return is not
> encountered then, by default, a 0 is returned (I believe).
> Is main an exception to the first paragraph above? I understand
> it is not--but I would like to be sure.
main() always has a return type of int. A main function with no
return statement returns 0 if it drops off the end.
Paul
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]