void main(void) illegal?? 
Author Message
 void main(void) illegal??

Quote:

>Somebody told me that

>void main(void)

>is illegal. Is that true?

Yes.  It results in undefined behavior.  It is covered in great detail in the
FAQ.  You might enjoy reading that document.

Quote:
>What about

>void main()

>is this illegal too??

Yes.  This is illegal too. Some compilers add this as an extension.  But this
code no longer conforms to the C standard.

Quote:
>If so, what is legal??!!

int main(void){return 0;}
int main(){return 0;}
main(){return 0;}
int main(int argc, char **argv){return 0;}
int main(int argc, char *argv[]){return 0;}
and several other permutations.  The C FAQ explains it in a special, tender,
loving way.  Get a copy and read it.  If you're not _chicken_.
Braak-braak-braak-braak-braak-braak!
--
Hypertext C-FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.*-*-*.com/


Fri, 11 Aug 2000 03:00:00 GMT  
 void main(void) illegal??


Quote:
> Somebody told me that

> void main(void)

> is illegal. Is that true?

Yes.

Quote:
> What about

> void main()

> is this illegal too??

These two forms are equal. :)  The problem with 'void main(void)' is not
in the parameter list; it's in the return type. main() -must- return an
int.

Quote:
> If so, what is legal??!!

int main(void)

and

int main(int argc, char **argv)

--
(initiator of the campaign for grumpiness where grumpiness is due in c.l.c)

Attempting to write in a hybrid which can be compiled by either a C compiler
or a C++ compiler produces a compromise language which combines the drawbacks
of both with the advantages of neither.



Fri, 11 Aug 2000 03:00:00 GMT  
 void main(void) illegal??

Somebody told me that

void main(void)

is illegal. Is that true?

What about

void main()

is this illegal too??

If so, what is legal??!!



Sat, 12 Aug 2000 03:00:00 GMT  
 void main(void) illegal??


Quote:
>Somebody told me that

>void main(void)

>is illegal. Is that true?

Um.  It's undefined.  That doesn't mean that your compiler won't do
something nice with it.  There is just no guarantee that all compilers
will do something nice with it.

It's not illegal, because no one will arrest you for it.  Unless, of
course, the resulting executable causes money to be transferred from
Bill Gates' checking account into a Swiss bank account in your name.
Then, you could be arrested for theft, because theft is illegal.
Since the behavior of your program is undefined, this might happen.

Quote:
>What about

>void main()

>is this illegal too??

No, it's not illegal.  It's still undefined though.

Quote:
>If so, what is legal??!!

int main (void)

int main (int argc, char *argv[])

I hope this helps.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Sat, 12 Aug 2000 03:00:00 GMT  
 void main(void) illegal??

By default, main() is supposed to return type int.  Generally, I believe

  main()

and

  int main()

are acceptable.  void main() may not be supported on a particular
implementation.  I personally use int main() just for clarity.

Arguements to void() are usually a number of arguements to main() and pointers
to strings (argc and argv, respectively).  

  int main ( int argc, char* argv[] )

Together, these two can be used to catch arguments to main().

- Alan


: Somebody told me that

: void main(void)

: is illegal. Is that true?

: What about

: void main()

: is this illegal too??

: If so, what is legal??!!



Sat, 12 Aug 2000 03:00:00 GMT  
 void main(void) illegal??



Quote:
>main(){return 0;}

Better not get in the habit.  In C9x we plan to do away
with implicit int.


Sat, 12 Aug 2000 03:00:00 GMT  
 void main(void) illegal??

Quote:

> Somebody told me that

> void main(void)

> is illegal. Is that true?

> What about

> void main()

> is this illegal too??

> If so, what is legal??!!

I always wonder, why questions like this are very likely to start
long threads of discussion and arguing in c.l.c

In my (pragmatic) opinion, it's not very important wether it's
'legal' or 'illegal' to use 'void main()' ...
I think it's more important to point out the hidden error in it,
which is, that the startupcode in the C-Runtime-Library
expects main() to return an int, which will be given to
the exit()-call which is done by the startupcode for the case main()
returns. If one uses void main() an undefined value will be used
for this exit() meaning, that the caller (shell/command.com/whoever)
can't rely on the result. Usually this result is used to indicate
successful/unsuccessful completion of the program.

Imagine you write a little tool, which you intend to use in a makefile...
'make' regards the exitcode 0 as ok, and everything else as the indication
of an error. So, using void main() for this tool (and not calling exit()
directly)
will force 'make' to stop executing the makefile in almost every case...
because it's very likely that the undefined result will be not zero.

BTH: I can't remember a compiler which will refuse to compile ...
   ... may be it needs the help of some 'nice' compilerswitches.
void main()
   {
   printf("void main: hello world\n");
   }
May be there might be some complains, but the generated code
will work so far. (Booking some dollars to Mr.B.G.'s account in
switzerland, as someone mentioned)

Regards Rainer



Sat, 12 Aug 2000 03:00:00 GMT  
 void main(void) illegal??



Quote:


>>main(){return 0;}

>Better not get in the habit.  In C9x we plan to do away
>with implicit int.

That's way cool!  What will the replacement be? Implicit intmax_t?


Sat, 12 Aug 2000 03:00:00 GMT  
 void main(void) illegal??

|> By default, main() is supposed to return type int.  Generally, I believe
|>
|>   main()
|>
|> and
|>
|>   int main()
|>
|> are acceptable.  void main() may not be supported on a particular
|> implementation.  I personally use int main() just for clarity.

As others have mentioned, the two legal forms are

    int main(void) { /* .. */ }
    int main(int argc, char *argv[]) { /* .. */ }

Other passages in the standard establish that both

    main() { /* .. */ }
    int main() { /* .. */ }

are equivalent to the first form above.  Nevertheless, as a mattter
of style, it's always best to use the full declaration as it is
invariably clearer.

Regards,

--
Chris Engebretson - Raytheon STX Corporation | Ph#: (605)594-6829
USGS EROS Data Center, Sioux Falls, SD 57198 | Fax: (605)594-6940

Opinions are not those of  Raytheon Systems Company  or the USGS.



Sat, 12 Aug 2000 03:00:00 GMT  
 void main(void) illegal??

|> It's not illegal, because no one will arrest you for it.  Unless, of
|> course, the resulting executable causes money to be transferred from
|> Bill Gates' checking account into a Swiss bank account in your name.
|> Then, you could be arrested for theft, because theft is illegal.

Well, it depends. If your program sends itself across the net to a
computer in some small Central European country where ripping off rich
Americans isn't explicitly forbidden, then everything is fine.

This is, of course, a quality-of-implementation issue.



Sat, 12 Aug 2000 03:00:00 GMT  
 void main(void) illegal??

Quote:

>Somebody told me that

>void main(void)

>is illegal. Is that true?

It's not illegal in any country or jurisdiction that I know about.
It is merely undefined as far as the C language goes.

C implementations are not required to anything meaningful with a program
that invokes undefined behavior. Such places in the language can be
used to provide extensions (though a void return value from main is rarely a
documented extension; it works by accident, or not at all).



Sat, 12 Aug 2000 03:00:00 GMT  
 void main(void) illegal??


#>Somebody told me that
#>void main(void)
#>is illegal. Is that true?

# It's not illegal in any country or jurisdiction that I know about.
# It is merely undefined as far as the C language goes.
# C implementations are not required to anything meaningful with a program
# that invokes undefined behavior.
[...]

It might be interesting to some of you that the most widely used
free compiler in its newest version does the following:

GNU C version 2.8.0 (sparc-sun-solaris2.5.1) compiled by GNU C version 2.8.0.
tst.c: In function `main':
tst.c:1: warning: return type of `main' is not `int'

What's more, it even refuses to compile the bullschildt when
the right options are selected.


tst.c: In function `main':
tst.c:1: return type of `main' is not `int'

                   ^
This is gcc's exit status. No executable was compiled.
It's good for your program's (and your) health to make
-pedantic-errors the default.

Wasn't that seebs hacking gcc in this way and giving the patch to
the GNU folks? Anyone enjoying void main (and otherwise void :-)
threads is invited to read the one on comp.std.c about public
comments on C9X.

Regards,

        Jens
--
Jens Schweikhardt  http://www.shuttle.de/schweikh
SIGSIG -- signature too long (core dumped)



Mon, 14 Aug 2000 03:00:00 GMT  
 void main(void) illegal??



Quote:
>It might be interesting to some of you that the most widely used
>free compiler in its newest version does the following:

>GNU C version 2.8.0 (sparc-sun-solaris2.5.1) compiled by GNU C version 2.8.0.
>tst.c: In function `main':
>tst.c:1: warning: return type of `main' is not `int'

Right on! Is this also in EGCS 1.0.1? I can't access my box from work right
now to verify.

I would have added ``, you dimwit!'' to that.

Whenever I write a parser, I make sure it has insulting messages. If a
semicolon is missing, I call the user an illiterate brute, for instance.
Or if some control flow construct is messed up, I accuse the user of
having been brain damaged by too much Visual Basic. Etc.

Most users seem to respond positively to such a thing---the ones who
don't take computer error messages personally, of course. :)



Mon, 14 Aug 2000 03:00:00 GMT  
 void main(void) illegal??



Quote:
>Wasn't that seebs hacking gcc in this way and giving the patch to
>the GNU folks?

Yup!

-s
--

C/Unix wizard, Pro-commerce radical, Spam fighter.  Boycott Spamazon!
Not speaking for my employer.  Questions on C/Unix?  Send mail for help.
Visit my new ISP <URL:http://www.plethora.net/> --- More Net, Less Spam!



Mon, 14 Aug 2000 03:00:00 GMT  
 void main(void) illegal??


Quote:



>>Wasn't that seebs hacking gcc in this way and giving the patch to
>>the GNU folks?

>Yup!

>-s
>--

>C/Unix wizard, Pro-commerce radical, Spam fighter.  Boycott Spamazon!

                                      ^^^^^^^^^^^^

Now, a little piece of Seebs fighting spam in your C programs inside each copy
of GCC!  

Quote:
>Not speaking for my employer.  Questions on C/Unix?  Send mail for help.
>Visit my new ISP <URL:http://www.plethora.net/> --- More Net, Less Spam!



Mon, 14 Aug 2000 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

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

2. main(void) vs void main ()

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

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

5. Why Is Void Main Illegal?

6. Why Is Void Main Illegal?

7. Purpose of void main (void) and Functions

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

9. void main(void) ????

10. void main(void)

11. Is void main(void) ANSI standard??

12. void main(void)

 

 
Powered by phpBB® Forum Software