Skip to main content
GameDev.net gamedev.net
🔒 Locked

void main()

Started by quant Jan 29, 2005 at 7:53 AM 7 replies 1.8k views
Original Post
quant
quant
Is void main() legal C or C++?
-keyboard
villiageidiot01
villiageidiot01
Is getting wasted if you are of legal age every weekend legal? Sure, but that doesn't make it the best thing to do, especially later on down the road. ;)

Likewise, don't void your mains. Use int main() or int main(int argc, char* argv[]) instead. http://www.research.att.com/~bs/bs_faq2.html#void-main ... if Bjarne Stroustrup doesn't void his mains, you shouldn't either.
quant
quant
But is it illegal?
-keyboard
flangazor
flangazor
It is legal C but severely deprecated.

[Edited by - flangazor on January 29, 2005 1:29:14 PM]
SirLuthor
SirLuthor
I believe it is legal in the sense that it will compile, but it certainly is a bad habit that should be pruned from the tree of knowledge before it flourishes, and leads to worse [smile] Cheers!
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Anon Mike
Anon Mike
The whole void main thing is close to be a religous issue in some circles. Technically speaking it's illegal (although the link doynax posted points out a loophole I wasn't aware of). However to make it even sillier the following *is* legal (at least in C99):

int main()
{
}

Note the lack of a return statement. In this case the compiler will automagically stick a return 0 in for you. This is nothing other than "void main" in disguise to confuse the whole issue. It would be a lot simpler for everybody if the various standards committees would have just allowed void main with the automagic return 0 and required int main to return something like every other function in the world that returns an int but I guess that would just make to much sense...
-Mike
doynax
doynax
Quote:
Original post by Anon Mike
It would be a lot simpler for everybody if the various standards committees would have just allowed void main with the automagic return 0 and required int main to return something like every other function in the world that returns an int but I guess that would just make to much sense...

I suppose that the main problem with that approach is that it might require special cases on architectures that handles functions without return values differently. Not that I can come up with an example of such an environment but surely there must be some esoteric DSP around, after all C still allows one's-complement implementations.
How would then use main as a function pointer? And what about freestanding systems where main() could be just any other function?
Both of these problems could of course still be solved with relative ease but not without ugly special case hacks, so my personal standpoint is compilers should just issue a warning and silently return 0 when possible.
Last, but definetly not least, there's still many ancient C compilers around so changing the standard now would result in even worse incompability problems than we're having today.

Imagine discovering that some small utility is returning random values and thus messing up your makefile process, just because the creator's favourite compiler let them get away with it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.