Date: Wed, 29 Oct 1997 15:57:42 -0700 (MST) From: firewind To: Peter Palotas cc: djgpp AT delorie DOT com Subject: Re: Reading the command line In-Reply-To: <3.0.16.19971029215033.35b7c09c@hem1.passagen.se> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On Wed, 29 Oct 1997, Peter Palotas wrote: > At 07.06 1997-10-29 GMT, you wrote: > >If you allow 'void main()' to be valid, you also allow 'if(1 + 1 == 3)' to be > >a true comparison. > > > >The standard says main() shall return int, and so int it shall return. > > If this is so, why doesn't the compiler generate an error for declaring > main() as 'void main()'? It works accidentally on some compilers (the DOS version of gcc, for example) and as an extension to the language on others (other popular DOS compilers, for example). In comp.lang.c, it has been brought up that some compilers (I think it was an HP\UX compiler, although I can't recall) *do* in fact flag 'void main()' as an error. The reason gcc does not vomit on 'void main()' (but it should, imho) is because the standard does not require it to. The standard says 'int main()' or 'int main(int argc, char *argv[])' (and 'int main(int argc, char **argv)' by congruence) are valid types for 'main.' Other declarations of main() invoke *undefined behavior*. The compiler can accept it, the compiler can vomit, the compiler can erase your hard drive, the compiler can print 'READ THE STANDARD, FOOL' fifty thousand times on your screen, and any of these are valid according to the standard. In other words, expecting 'void main' to work is just about as foolish as expecting 'a[i++] = i++;' to yield a predictable result. This (modifying and using a variable more than once without a sequence point) is also undefined behavior, and the compiler is not required to (but can!) refuse to accept it. If you would like more and detailed discussion of why 'void main' is illegal, and why some people refuse to let go of it, there have been several threads of this topic going on in comp.lang.c. See DejaNews. [- firewind -] [- email: firewind AT metroid DOT dyn DOT ml DOT org (home), firewind AT aurdev DOT com (work) -] [- "You're just jealous because the voices talk to -me-." -] [- Have a good day, and enjoy your C. -] [- (on a crusade of grumpiness where grumpiness is due) -]