Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Return type - main() / ISO C standard (http://www.programmingforums.org/showthread.php?t=12793)

sharadpro Mar 14th, 2007 4:46 AM

Return type - main() / ISO C standard
 
The ISO C++ standard clearly wants the return type of main to be int, so there is no conflict there.But quoting from the link given below,ISO C standard doesn't say so :

Quote:

The ISO C++ Standard (ISO/IEC 14882:1998) specifically requires main to return int. But the ISO C Standard (ISO/IEC 9899:1999) actually does not. This comes as a surprise to many people. But despite what many documents say, including the Usenet comp.lang.c FAQ document (at great length), the actual text of the C Standard allows for main returning other types.
http://homepages.tesco.net/J.deBoyne...void-main.html

I completely understand that void main() is EVIL anyways but please clarify the issue as per the C standard!

Jimbo Mar 14th, 2007 5:16 AM

From some draft of the standard I have in PDF form:
Quote:

1 The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;(9) or in some other implementation-defined manner.
.....
(9) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as
char ** argv, and so on.
The actual standard might be slightly revised, but I'm almost certain the return type of main still has to be compatible with int.

grumpy Mar 14th, 2007 7:02 AM

There are some people who quibble over the placement of a semi-colon in the C standard, claiming that the way it is placed allows main() to be implemented in ways other than returning int. The people who do that are attempting to say that main() not returning int is still standard C. They are talking from their nether regions.

The C and C++ standards are worded differently (mainly because the C++ standard has to be unambiguous and prevent things like overloading of main() by a programmer). However, both are quite clear in requiring compilers to support main() in both the "int main()" and "int main(int argc char *argv[])" [or equivalent] forms, and that any other form of main() is implementation defined behaviour.

Which means that both C and C++ compilers are required to support the int main() forms, or they cannot be described as standard compliant. Other forms represent implementation defined behaviour: a compiler is allowed to support any form of main() it wishes as long as it also supports the two int forms.

User code, on the other hand, cannot be compliant with the standard if it uses any form of main() other than the int forms. In english, this means that code which uses any form of main, other than the two int forms, is compiler specific.


All times are GMT -5. The time now is 2:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC