Is void main(void) ANSI standard?? 
Author Message
 Is void main(void) ANSI standard??

This is my first posting to this group ( I read the FAQ first!!).

My question is basic and regarding the function main().

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?  If it is valid, why doesn't my SUN SparcStation like
it?  Is it possible to write code that will not kick out ANY warning messages
when compiled, regardless of the platform or error message level??  I write for
Borland (PC), UNIX (HP & SUN), as well as VMS (VAX) and this is giving me
fits!!

Please reply by E-Mail and I will summarize if there is sufficient interest.

                        Mark  <o===6



Fri, 27 Aug 1993 16:54:39 GMT  
 Is void main(void) ANSI standard??

Quote:
>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?  If it is valid, why doesn't my SUN SparcStation like
>it? [...]

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.

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.

Of course, you should see another thread appearing in this group for a
discussion about return values under VMS.

--



Sat, 28 Aug 1993 09:35:41 GMT  
 Is void main(void) ANSI standard??

Quote:

> > 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 !

Quote:
> 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 :)




Sat, 28 Aug 1993 15:41:59 GMT  
 Is void main(void) ANSI standard??

Quote:
>Is this ANSI standard?

No, there are only two strictly conforming interface definitions your
program can use, int main(void) and int main(int argc, char **argv).
You should always terminate by invoking the exit function or by
returning a value from the main function.  The return value zero is
appropriate when you are not trying to report an error to the environment.


Sun, 29 Aug 1993 02:34:52 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. what's the difference between void main(void) and int main(void)

2. Is void main(void) a standard or not?

3. void main(), ANSI standard?

4. main(void) vs void main ()

5. void main(void) or int main() ??

6. from void (void*) to void (_cdecl*) (void*)

7. Purpose of void main (void) and Functions

8. void main(void) ????

9. void main(void) illegal??

10. void main(void)

11. void main(void)

12. void main(void)....revisited!

 

 
Powered by phpBB® Forum Software