Topic: the return type of main function
Author: Ahn Ki-yung <kyagrd@chiak.kaist.ac.kr>
Date: 1999/05/06 Raw View
I heard that in C++ Standard the return type of main is now
restricted to be int. If it is true why is it so ?
Why shouldn't we just use void ? Is there any reason for this ?
p.s. Sorry, question is too simple question. :-)
I'll try to bring good subject to discuss.
[ 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: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/05/07 Raw View
On 6 May 1999 19:59:02 GMT, Ahn Ki-yung <kyagrd@chiak.kaist.ac.kr> wrote:
>I heard that in C++ Standard the return type of main is now
>restricted to be int. If it is true why is it so ?
>Why shouldn't we just use void ? Is there any reason for this ?
Your program, represented by the function main(), is just another function
in the big picture. The value returned by main() signifies whether the
program was successful -- zero means success, non-zero means failure.
This means that your program can be used as part of a bigger program, like
a script file. Another example: make invokes the compiler to compile a
program (eg, "CC -c file1.cc"), and if the compile was successful, it goes
on to compile a second program ("CC -c file2.cc").
Hopefully in the future, operating systems will know how to handle
exceptions. If this happens, this means that main() will be able to
throw exceptions.
>p.s. Sorry, question is too simple question. :-)
> I'll try to bring good subject to discuss.
This topic is beaten to death in comp.lang.c++.
Check www.dejanews.com, under power search.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/05/07 Raw View
In article <3731E2DB.4B86C9BF@daidun.kaist.ac.kr>, Ahn Ki-yung
<kyagrd@chiak.kaist.ac.kr> writes
>I heard that in C++ Standard the return type of main is now
>restricted to be int. If it is true why is it so ?
>Why shouldn't we just use void ? Is there any reason for this ?
1) It was never otherwise.
2) main exits by calling exit() which receives the returned value from
main. Of course we can use some magic to fix this problem, but why
should we? Writing return 0; at the end of main is hardly any more
tiresome, and to help the lazy the C++ Standard states that falling out
of main without an explicit return is equivalent to return 0 (but a
certain well known compiler cannot (or could not when I last checked)
manage that either.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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: Ron Natalie <ron@sensor.com>
Date: 1999/05/07 Raw View
Ahn Ki-yung wrote:
>
> I heard that in C++ Standard the return type of main is now
> restricted to be int. If it is true why is it so ?
> Why shouldn't we just use void ? Is there any reason for this ?
>
It has never been the case that a specification of the C and C++
languages allowed main to return anything other than int. It
has just been the case that on most implementations the calling
sequence for functions returning int and functions returning
void have been similar enough that you could get away with it.
It is the case that the caller and the callee must agree
on the arguments and return type of a function. The caller
expects main to return int, you can not arbitrarily change
it and expect things to work.
[ 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 ]