main(void) vs void main () 
Author Message
 main(void) vs void main ()

Hi,

I have just began learning the C language and one of the problems I
encountered was that some compilers use

void main ()

while other compilers use

main (void)

can someone explain the difference please.

Thanks

Adrian



Thu, 05 Feb 2004 17:10:39 GMT  
 main(void) vs void main ()

Quote:
> Hi,
> I have just began learning the C language and one of the problems I
> encountered was that some compilers use
> void main ()
> while other compilers use
> main (void)
> can someone explain the difference please.

DISCLAIMER: Neither form is standard. The only standard forms of main()
are
int main(void)
and
int main(int argc, char *argv[]).

Now, on to your question:
The first form, void main(), means that main() never returns anything.
It is a horrible butchering of the C language and should never be used
anywhere. There is nothing whatsoever to be gained by using void main().
The people who use it in their examples are clueless twits who only want
to shut up their compiler warnings.
The second form, main(void), means that main() is implicitly declared to
return an int, and takes no parameters. In the days when K&R ruled the
land alone, without the ANSI standard, this was standard. The ANSI C
standard abolished implicit int, so it should no longer be used either.
The people who use it in their examples are old fogies who were taught
by K&R personally and haven't noticed the language has changed since.

In a nutshell, the difference is once more:
void main() doesn't return anything. main(void) used to return int.

The m{*filter*}of the story:
Don't use either of them. Use int main(void) or int main(int argc,
char *argv[]).

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.*-*-*.com/ ~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
   - Karri Kalpio



Fri, 06 Feb 2004 14:51:33 GMT  
 main(void) vs void main ()

Quote:

> I have just began learning the C language and one of the problems I
> encountered was that some compilers use

> void main ()

> while other compilers use

> main (void)

> can someone explain the difference please.

I don't know what you mean by "use".  Compilers don't write your
source code for you.  You write the source code.  Do you mean
that some compilers expect that form, or that they accept it?
That said, I will make some comments.

The first example you give is:

   void main () { /* Definition of main goes here. */ }

This is illegal.  This says that main returns nothing (from
the "void" at the beginning), but main is required to return
an int.  [It also uses the old-style notation "()" to indicate
that main uses no arguments.  That is legal, but deprecated.]

The second example you give is:

   main (void) { /* Definition of main goes here. */ }

Since no return type is specified for main, it defaults to int.
That is correct, but also deprecated.  It is better to specify
the return type explicitly.  The "(void)" is the new-style
way of indicating that main uses no arguments.

There are two ways that you SHOULD define main.  Which you use
depends on whether or not you want main to use any command-line
arguments that may be passed to it.

If you do NOT want main to use command-line arguments, then
define it like this:

   int main (void) { /* Def. of main goes here. */ }

If you DO want main to use command-line arguments, then
define it like this:

   int main (int argc, char ** argv) { /* Def. of main goes here. */ }

In both cases, the last statement in main before the closing brace
should be one of the following three:

   return 0;
   return EXIT_SUCCESS;
   return EXIT_FAILURE;

The first has the same effect as the second.  However, the second and
third require you to have the following at the top of your code
(before the definition of main):

   #define <stdlib.h>

Hope this clears things up.



Fri, 06 Feb 2004 15:16:56 GMT  
 main(void) vs void main ()

Quote:

> The second form, main(void), means that main() is implicitly declared to
> return an int, and takes no parameters. In the days when K&R ruled the
> land alone, without the ANSI standard, this was standard. The ANSI C
> standard abolished implicit int, so it should no longer be used either.

This question is somewhat academic, since I would never use the
"implicit int" feature anyway, but I just wanted to know:
Did the C89 standard really make this illegal, or merely deprecated?


Fri, 06 Feb 2004 15:27:46 GMT  
 main(void) vs void main ()

Quote:

> I have just began learning the C language and one of the problems I
> encountered was that some compilers use

> void main ()

> while other compilers use

> main (void)

> can someone explain the difference please.

if a function f is defined as:-
    void f ()
the function takes no arguments and doesn't return anything.
Better style would be:-
    void f (void)

if it is defined as
    f (void)
it takes no arguments and returns int. This applies to the older 1989 C
standard (often abreviated to C89). In the latest 1990 standard (C90) the
above is not allowed. The better form for C89 and only allowable form for C90
is:-
    int f (void)

Now back to main(). main() *always* returns an int. Hence the allowable forms are:-
    int main (void)
    int main (int argc, char *argv [])

in C89 the can be ommitted, but is poor style. In C90 it cannot be ommitted.
Anything that starts "void main" is wrong. I suggest you get a decent book
on C.



Fri, 06 Feb 2004 17:48:34 GMT  
 main(void) vs void main ()

Quote:

> I have just began learning the C language and one of the problems I
> encountered was that some compilers use
> void main ()

<snip>

All (conforming) hosted implementations (compilers) use

int main(void)
or
int main(int argc, char **argv)  /* or equivilent */

An implementation may define other means of defining main, but they are
under no obligation (by the standard) to do so.

Bill, avoid maintainance.



Fri, 06 Feb 2004 19:42:33 GMT  
 main(void) vs void main ()

Quote:

> I have just began learning the C language and one of the problems I
> encountered was that some compilers use

> void main ()

> while other compilers use

> main (void)

> can someone explain the difference please.

Another failure to read the FAQ.  "void main" subjects just went
back into my PLONK file.

--

   (Remove "XXXX" from reply address. yahoo works unmodified)



Fri, 06 Feb 2004 21:38:31 GMT  
 main(void) vs void main ()
there shouldn't be any different. the only thing is that the
directions to the compiler can be different. u can ask the compiler to
give a warning about any function like "void f()" for not being "void
f(void)" (but not stop compiling because of that).
the function "main" should be defined as returning "int" but can
return "void" too, so the compiler can get the 2 ways and not warn
about any of them.
for C, there is no difference.
Quote:

> Hi,

> I have just began learning the C language and one of the problems I
> encountered was that some compilers use

> void main ()

> while other compilers use

> main (void)

> can someone explain the difference please.

> Thanks

> Adrian



Fri, 06 Feb 2004 22:37:49 GMT  
 main(void) vs void main ()

Quote:
> there shouldn't be any different. the only thing is that the
> directions to the compiler can be different. u can ask the compiler to
> give a warning about any function like "void f()" for not being "void
> f(void)" (but not stop compiling because of that).
> the function "main" should be defined as returning "int" but can
> return "void" too, so the compiler can get the 2 ways and not warn
> about any of them.
> for C, there is no difference.

Please stop talking rubbish. As already established in this thread,
"void f()" means that f() never returns anything, while "f()" or
"f(void)" is an obsolete way of telling that f() returns int.
Your claim "the function \"main\" should be defined as returning
\"int\" but can return \"void\" too" is only partially correct.
main SHOULD be defined as returning int, but it can't return void.
The people who are trying to make it do that are creating undefined
behaviour.
For C, there is a huge difference. Or what do you call the difference
between "It has to work every time" and "It just might work, but then
again, it might not, who knows?"?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"The truth is out there, man! Way out there!"
   - Professor Ashfield



Fri, 06 Feb 2004 22:58:12 GMT  
 main(void) vs void main ()

Quote:

> Hi,

> I have just began learning the C language and one of the problems I
> encountered was that some compilers use

> void main ()

> while other compilers use

> main (void)

> can someone explain the difference please.

> Thanks

> Adrian

The first form defines main as a function taking an unspecified number
of arguments and a having return type of void.  The second form
defines main as a function taking no arguments and having an implied
return type of int.

There are two legal definitions for main() defined by the C Standard
(as of C89, anyway; I'm not sure about C99):

int main (void) {}
int main (int argc, char **argv) {}  

Since the first form (void main()) is not one of the definitions in
the Standard, using it invokes something called "undefined behavior",
where the compiler is free to do anything it wants with the construct.
 Most of the time, the compiler will generate something reasonable and
the program will appear to compile and run correctly, but there are
cases where using void main() can cause runtime problems.

Undefined behavior is a Bad Thing and should be avoided.  

Note that the definition of main only matters on hosted
implementations.  If you don't know what a hosted implementation is,
then you are likely using one.

There is a very long and contentious thread about using void main()
called, appropriately enough, "void main(void)".



Fri, 06 Feb 2004 23:21:13 GMT  
 main(void) vs void main ()

Quote:

> there shouldn't be any different. the only thing is that the
> directions to the compiler can be different. u can ask the compiler to
> give a warning about any function like "void f()" for not being "void
> f(void)" (but not stop compiling because of that).
> the function "main" should be defined as returning "int" but can
> return "void" too, so the compiler can get the 2 ways and not warn
> about any of them.
> for C, there is no difference.

WRONG! If you read the standard for C, you will note that main
always returns an int. If you define it as 'void main...', then you
are likely to have many problems, including errors when the program
exits and portability problems. Many modern compilers will flag
'void main' as an error.

The 'void main' is, however, a good indicator of a clueless newbie
using one of the Schildt books. Hopefully that stage doesn't last
too long.

Please check the 'void main' section in the FAQ.



Fri, 06 Feb 2004 22:42:00 GMT  
 main(void) vs void main ()

[Talking about implicit int]

Quote:
>    This applies to the older 1989 C
>standard (often abreviated to C89). In the latest 1990 standard (C90) the
>above is not allowed.

Nick of course meant to say `In the latest 1999 standard (C99)...'

This is the second consecutive post in this thread with a small but
significant error made by a regular who should know better.  Is it
September already?  Or is it just that posting to void main threads has
the same effect as having one's brain removed?  (And in the latter
case, is there any hope for me?)

dave

--

Quote:
> which bit of "Can we just not go there again please" did not make sense?

I think it was the "please" that threw me.
                     --Mark McIntyre and Richard Heathfield in comp.lang.c


Fri, 06 Feb 2004 23:13:49 GMT  
 main(void) vs void main ()

Quote:

> Hi,

> I have just began learning the C language and one of the problems I
> encountered was that some compilers use

> void main ()

There is no C compiler for a hosted implementation that "uses" this.
There are compilers that accept this illegal construct, just to keep
clueless users happy.  That is no reason to write such illegal code
yourself.

Quote:

> while other compilers use

> main (void)

This is, under the old standard, equivalent to

   int main(void)

The new standard requires the explicit int, so use it.

Quote:

> can someone explain the difference please.

The first is not now and has never been legal.
The second has been legal (under the implicit int rule) and is now
   illegal without the explicit int.


Sat, 07 Feb 2004 02:49:46 GMT  
 main(void) vs void main ()


Quote:
> Hi,
> I have just began learning the C language and one of the problems I
> encountered was that some compilers use
> void main ()
> while other compilers use
> main (void)
> can someone explain the difference please.

Well, they have something in common in that they are both non-standard.
Better write main as either int main( void ) or int main( int argc, char*
argv[] ).


Sat, 07 Feb 2004 03:20:02 GMT  
 main(void) vs void main ()

Quote:

<snip>

> Undefined behavior is a Bad Thing and should be avoided.

Yes and no. In the current instance (void main()), you are absolutely
right. Zero benefit, portability cost. There are other kinds of
undefined behaviour, too - such as, for example:

int i = getch();

Here, the portability cost is (presumably) outweighed by the program's
need for an unbuffered keystroke.

Quote:
> Note that the definition of main only matters on hosted
> implementations.

I disagree, I'm afraid. It matters just as much on freestanding
implementations, insofar as you should use (one of) the definition(s) of
main() - or its equivalent! - provided by the implementation's
documentation.

Quote:
> If you don't know what a hosted implementation is,
> then you are likely using one.

:-)

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Sat, 07 Feb 2004 03:44:37 GMT  
 
 [ 22 post ]  Go to page: [1] [2]

 Relevant Pages 

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

2. main() vs int main() vs int main(void)

3. What's the difference: int main(void) VS void main(void)?

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

5. int main vs. void main

6. int main() vs int main(void)

7. newbie question....int main, void main, main

8. people who are bitter over void main(void) vs. int main(void)

9. Purpose of void main (void) and Functions

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

11. void main(void) ????

12. void main(void) illegal??

 

 
Powered by phpBB® Forum Software