Topic: Does "main()" always return an int ?
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/01/27 Raw View
Steve Clamage wrote:
>
> In article BDC47897@ix.netcom.com, "Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
> >Does this mean that you can also put an explicit "return;" in main(), and have
> >it interpreted as "return 0;"?
>
> No. The program would be ill-formed (6.6.3 "The return statement").
I'm not surprised. But it does seem that they've introduced a rather strange
contradiction into the language, and that it might have been cleaner had they
also allowed "return;" as well.
--
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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1998/01/21 Raw View
In article <01bd23be$33c440e0$480b48a6@aptiva>, drobri@ibm.net says...
> I have a question for the group. Does "main()" always return an int ? In
> Microsoft Visual C++ "main()" is allowed to return void. Some books on C++
> programming also use the "void main()" function definition. Solaris C++
> and Borland C++ 5.0 seem only to mention the "int main()" function
> definition. The ARM book also only mentions the "int main()" function
> definition.
Each implementation must support both:
int main(); // or equivalently int main(void);
and
int main(int argc, char **argv); // or equivalently, `char *argv[]'
There may be other implementation defined forms of main supported as
well.
--
Later,
Jerry.
The Universe is a figment of its own imagination.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/01/22 Raw View
In article BDC47897@ix.netcom.com, "Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
>Herb Sutter wrote:
>>
>> Note also that C++ allows an implicit "return 0;" at the end of main() if
>> you don't write a return statement yourself. Therefore there's no reason,
>> not even 'simplicity,' to ever write void main() -- int main() will always
>> do just as nicely, and it is standard. (Beware also any claims that you
>> have to write return 0; explicitly.)
>
>Does this mean that you can also put an explicit "return;" in main(), and have
>it interpreted as "return 0;"?
No. The program would be ill-formed (6.6.3 "The return statement").
Section 3.6.1 "Main function" says that main must have a return type
of int, and that if you reach the end of the function without
encountering a return statement, the effect is that of executing
return 0;
The point of this language change was to recognize existing practice.
It has been the case for many years that many C and C++ compilers did
not require a return statement in main, and that if it were omitted
the implementation arranged for the effect of a zero return. The
C++ standard now codifies that behavior. (The C9X draft seems not have
added this feature.)
---
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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: pilgrim@earth.gov (Sojourner Truth)
Date: 1998/01/22 Raw View
yes
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Don R. O'Brien" <drobri@ibm.net>
Date: 1998/01/18 Raw View
I have a question for the group. Does "main()" always return an int ? In
Microsoft Visual C++ "main()" is allowed to return void. Some books on C++
programming also use the "void main()" function definition. Solaris C++
and Borland C++ 5.0 seem only to mention the "int main()" function
definition. The ARM book also only mentions the "int main()" function
definition.
So what does the new C++ standard say? How is "main()" defined ?
Thanks in Advance
Don R. O'Brien
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: herbs@cntc.com (Herb Sutter)
Date: 1998/01/19 Raw View
"Don R. O'Brien" <drobri@ibm.net> wrote:
>I have a question for the group. Does "main()" always return an int ? In
>Microsoft Visual C++ "main()" is allowed to return void. Some books on C++
>programming also use the "void main()" function definition. Solaris C++
>and Borland C++ 5.0 seem only to mention the "int main()" function
>definition. The ARM book also only mentions the "int main()" function
>definition.
>
>So what does the new C++ standard say? How is "main()" defined ?
Since the C days, main returns int, period. Now, "void main()" is a common
extension supported by many C and C++ compilers, but it's just that: an
extension. The only two standard signatures of main are int main() and int
main( int, char*[] ), both of which clearly return int. (Beware any claims
that any of this is news.)
Note also that C++ allows an implicit "return 0;" at the end of main() if
you don't write a return statement yourself. Therefore there's no reason,
not even 'simplicity,' to ever write void main() -- int main() will always
do just as nicely, and it is standard. (Beware also any claims that you
have to write return 0; explicitly.)
---
Herb Sutter (mailto:herbs@cntc.com)
Current Network Technologies Corp. 2695 North Sheridan Way, Suite 150
www.cntc.com www.peerdirect.com Mississauga Ontario Canada L5K 2N6
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/01/19 Raw View
"Don R. O'Brien" <drobri@ibm.net> writes:
>I have a question for the group. Does "main()" always return an int ? In
>Microsoft Visual C++ "main()" is allowed to return void. Some books on C++
>programming also use the "void main()" function definition. Solaris C++
>and Borland C++ 5.0 seem only to mention the "int main()" function
>definition. The ARM book also only mentions the "int main()" function
>definition.
>So what does the new C++ standard say? How is "main()" defined ?
The only definitions of function main sanctioned by the C or C++
standards all return an int value to the entity that invokes
the main program.
Implementations are allowed to provide or support other definitions
of main. If an implementation does allow other definitions, you can't
depend on them being available elsewhere. Textbook authors in
particular are in my view wrong-headed if they show programs with
anything but "int main".
You would have to read the documentation to find out what non-int
definitions of "main" mean. For example, you are supposed to be
able to invoke a C or C++ program, and test the return value,
presumed to be an int. I wonder what result you could expect from
such a test if the programmer defined main to return no value.
--
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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Pete Becker <petebecker@acm.org>
Date: 1998/01/19 Raw View
Don R. O'Brien wrote:
>
> I have a question for the group. Does "main()" always return an int ? In
> Microsoft Visual C++ "main()" is allowed to return void. Some books on C++
> programming also use the "void main()" function definition. Solaris C++
> and Borland C++ 5.0 seem only to mention the "int main()" function
> definition. The ARM book also only mentions the "int main()" function
> definition.
>
> So what does the new C++ standard say? How is "main()" defined ?
Standard C++ requires that main return int. Many compilers won't
complain if main returns void, and that's a legitimate extension. Many
careless writers give code examples that have main returning void. That
is wrong, because it confuses people.
-- Pete
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]