Advertisement

whats the point of -> void main();

Started by November 22, 2000 11:40 AM
24 comments, last by Tsu 24 years, 1 month ago
quote:
if "void main()" violated anything, then i don''t think it would be allowed.


It''s not allowed, that''s point. At least not the C & C++ standard, an implementation of C/C++ can allow it if it wants, but that doesn''t mean it''s valid C or C++.
Okay, theoretically windows doesn''t use your return type but...

If you use void main() and windows for one reason or another trys to pull a return value from the stack when your prog finishes it will be looking for a nonexistent value on the stack. How do you spell GPF?
Bottom Line: Always use int main() its easy to have return 0; and also you never know when your stuff could get opened on some older system or compiler where it could crash and burn.
Advertisement
Um, not returning a value from main() or WinMain() will NOT cause your program to GPF. main() is not the end all be all of your program - its up to Windows, not your program, to cleanup your thread (and hence retrieve the thread''s exit code). All programs consist of at least a process kernel object and a thread kernel object. Statistical information about your process and associated thread are managed by Windows not your program.

Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
I always use int main() because some times I want to be able to end my program somewhere in the middle of the actual code (ie. after a conditional) and its much easier to just write return(0); instead of BREAKing all the functions/loops its currently in. As for getting errors using void main(); I''ve never seen it but Im probably less experienced than these guys.
codeXtremeif(you->intelligence < radish->intelligence) you->STFU();
If that''s the only reason that you''re using a return value, you realize that you can always just use a bare return; return doesn''t need to take an argument. Or if you''re trying to terminate the program as a whole, use exit(). Though exit needs to take a status value.
Question 11.2 of the C FAQ:

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

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

The Micro$haft BSOD T-Shirt
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt

This topic is closed to new replies.

Advertisement