Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!iguana.cis.ohio-state.edu!meranda From: meranda@iguana.cis.ohio-state.edu (deron meranda) Newsgroups: comp.lang.c Subject: Re: Is void main(void) ANSI standard?? Message-ID: <94479@tut.cis.ohio-state.edu> Date: 12 Mar 91 07:41:59 GMT References: <1991Mar11.085439.1@sysjj.mdcbbs.com> <1135@caslon.cs.arizona.edu> Sender: news@tut.cis.ohio-state.edu Reply-To: deron meranda Organization: Ohio State University Computer and Information Science Lines: 50 In article <1135@caslon.cs.arizona.edu> dave@cs.arizona.edu (Dave P. Schaumann) writes: > In article <1991Mar11.085439.1@sysjj.mdcbbs.com> hooverm@sysjj.mdcbbs.com (SQUID 6 on the DARK side) writes: > > When a program is compiled/linked and there is no value returned to main, > > certain platforms will return a warning message stating that this function > > doesn't return a value. > > > > I thought that by declaring: void main(void) that I would get around this. > > > > Is this ANSI standard? [...] > > Well, first you have to understant that one of the oddities of C (IMHO) is > that if you declare a function like this: > > main() { ... } > > The return type becomes "int" by implication. This is probably not the best > idea in the world, but the backward-compatability crowd wouldn't have it > any other way. The "implied int" kinda reminds me of FORTRAN, yuck ! > Declaring main as a void is *not* a good idea. Many systems take the value > returned by main() to be the "exit status" of the program. (Usually, a > value of 0 means all is well, and a non-0 value means some kind of error > has occurred). If you don't really care about this, just put a return 0 ; > (or exit(0)), and all should be well. [...] Anyway, to be ANSI conformant, main must be declared as one of: int main( void ) or int main( int argc, char *argv[] ) Declaring main as "returning" void is not standard. Furthermore, if main returns without returning a value back, the results are undefined. Any platform is permitted to allow this "no return value" as an extension, but in general it should be avoided. The function main is confusing at first, because unlike most other functions, it has TWO legal declarations! As for returning a harmless value, you should #include and use either return(EXIT_SUCCESS) or exit(EXIT_SUCCESS) from main. Both the return and exit methods are equivalent. It is true that in most systems, EXIT_SUCCESS is defined as 0, but not all! Whenever you get a compiler warning about no return value, or return value type mismatches, in most cases it is probably safer to try to correct the problem by supplying a correct return value, rather than trying to redefine the declaration of the function itself :) Deron E. Meranda ( meranda@cis.ohio-state.edu )