Mail Archives: djgpp/1997/07/05/18:17:32
Chris Croughton wrote:
>
> Where in the ANSI C standard does it say that main
> must have type 'int'? I can't find it.
>
> It may say so in the DRAFT C++ standard, but that is
> not yet approved as far as I am aware.
>
> Please quote the section, because I don't believe that
> it was put into the ANSI C standard.
You're obviously missing something. The comp.lang.c Frequently Asked
Questions list gives a complete set of ANSI references in question 11.12
(http://www.eskimo.com/~scs/C-faq/q11.12.html), which dicusses why void
main() is not a good idea.
Here's a direct quote:
--snip--
Question 11.12
Can I declare main as void, to shut off these annoying ``main returns no
value'' messages?
No. main must be declared as returning an int, and as taking either zero
or two arguments, of the appropriate types. If you're calling exit() but
still getting warnings, you may have to insert a redundant return
statement (or use some kind of ``not reached'' directive, if available).
Declaring a function as void does not merely shut off or rearrange
warnings: it may also result in a different function call/return
sequence, incompatible with what the caller (in main's case, the C
run-time startup code) expects.
(Note that this discussion of main pertains only to ``hosted''
implementations; none of it applies to ``freestanding'' implementations,
which may not even have main. However, freestanding implementations are
comparatively rare, and if you're using one, you probably know it. If
you've never heard of the distinction, you're probably using a hosted
implementation, and the above rules apply.)
References: ANSI Sec. 2.1.2.2.1, Sec. F.5.1
ISO Sec. 5.1.2.2.1, Sec. G.5.1
H&S Sec. 20.1 p. 416
CT&P Sec. 3.10 pp. 50-51
--snip--
See also the next four questions in the FAQ.
--
---------------------------------------------------------------------
| John M. Aldrich | "Sin lies only in hurting other |
| aka Fighteer I | people unnecessarily. All other |
| mailto:fighteer AT cs DOT com | 'sins' are invented nonsense." |
| http://www.cs.com/fighteer | - Lazarus Long |
---------------------------------------------------------------------
- Raw text -