Is void main(void) ANSI standard??

deron meranda meranda at iguana.cis.ohio-state.edu
Tue Mar 12 17:41:59 AEST 1991


In article <1135 at caslon.cs.arizona.edu> dave at cs.arizona.edu (Dave P. Schaumann) writes:
> In article <1991Mar11.085439.1 at sysjj.mdcbbs.com> hooverm at 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 <stdlib.h> 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 at cis.ohio-state.edu )



More information about the Comp.lang.c mailing list