Mail Archives: djgpp/1997/05/25/14:32:26
nikki wrote:
>
> why pray?
> void main() has always worked before. works fine under gcc, cc and
> every other c compiler i've ever touched. what's with the '*must*
> return an int' stuff?
enough is enough. i am not affiliated with the pgcc project. however,
for the sake of public education, here is the relevant quote from the C
FAQ. i mean, if you are going to program in C, read that at least.
you can get it from:
http://www.cis.ohio-state.edu/hypertext/faq/usenet/C-faq/faq/faq.html
11.12: Can I declare main() as void, to shut off these annoying "main
returns no value" messages?
A: 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.
11.14: I believe that declaring void main() can't fail, since I'm
calling exit() instead of returning, and anyway my operating
system ignores a program's exit/return status.
A: It doesn't matter whether main() returns or not, or whether
anyone looks at the status; the problem is that when main() is
misdeclared, its caller (the runtime startup code) may not even
be able to *call* it correctly (due to the potential clash of
calling conventions; see question 11.12). Your operating system
may ignore the exit status, and void main() may work for you,
but it is not portable and not correct.
11.15: The book I've been using, _C Programing for the Compleat Idiot_,
always uses void main().
A: Perhaps its author counts himself among the target audience.
Many books unaccountably use void main() in examples. They're
wrong.
11.16: Is exit(status) truly equivalent to returning the same status
from main()?
A: Yes and no. The Standard says that they are equivalent.
However, a few older, nonconforming systems may have problems
with one or the other form. Also, a return from main() cannot
be expected to work if data local to main() might be needed
during cleanup; see also question 16.4. (Finally, the two forms
are obviously not equivalent in a recursive call to main().)
References: K&R2 Sec. 7.6 pp. 163-4; ANSI Sec. 2.1.2.2.3; ISO
Sec. 5.1.2.2.3.
--
Sinan
*******************************************************************
A. Sinan Unur WWWWWW
|--O+O
mailto:sinan DOT unur AT cornell DOT edu C ^
http://www.people.cornell.edu/pages/asu1/ \ ~/
Unsolicited e-mail is _not_ welcome, and will be billed for.
*******************************************************************
- Raw text -