void main() or int main()? 
Author Message
 void main() or int main()?

I've been a C/C++ user for 2-3 years now. Whenever i need to write
small console/shell applications, i always declare main's type as
void, ie "void main(int argc, char *argv[]). However i've noticed a
quite some people on usenet insisting that "void main()" should be
"int main()".

I personally feel that there is no difference, and that its a matter
of preference. (Unless,of course, u do want main to return something).
so whats the difference? does that C++ standard mandate anything about
main's return type? and whats the position of the c99 standard on
this?

---
Black holes result when God
divides the universe by zero
---



Sat, 04 Dec 2004 18:58:25 GMT  
 void main() or int main()?

on comp.lang.c:

Quote:
> I've been a C/C++ user for 2-3 years now. Whenever i need to write
> small console/shell applications, i always declare main's type as
> void, ie "void main(int argc, char *argv[]). However i've noticed a
> quite some people on usenet insisting that "void main()" should be
> "int main()".

Those people are right. void main() has never been an allowed form of
the main function. The only possible return types for main() are int
and implicit int.

Quote:
> I personally feel that there is no difference, and that its a matter
> of preference. (Unless,of course, u do want main to return something).
> so whats the difference? does that C++ standard mandate anything about
> main's return type? and whats the position of the c99 standard on
> this?

The difference is that int main() is standard and void main() is not.
The C89, C90 and C99 standards are identical on this. Even K&R C did not
allow void main(). If you use void main() in your program, the behaviour
is automatically undefined, and the program may do anything at all, and
you don't have the right to complain about it.

--

| 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 day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
   - Ernst Jan Plugge



Sat, 04 Dec 2004 19:10:39 GMT  
 void main() or int main()?


Quote:
> I've been a C/C++ user for 2-3 years now. Whenever i need to write
> small console/shell applications, i always declare main's type as
> void, ie "void main(int argc, char *argv[]). However i've noticed a
> quite some people on usenet insisting that "void main()" should be
> "int main()".

> I personally feel that there is no difference, and that its a matter
> of preference. (Unless,of course, u do want main to return
something).
> so whats the difference? does that C++ standard mandate anything
about
> main's return type? and whats the position of the c99 standard on
> this?

    In the C++ Standard it says that only main must return int. But
main does not need the "return 0;" line, since that would be done
implicitly (and VC++6 is broken in that respect). If you have main
return void, what would the return flag be? I mean, the OS might rely
on the return value of your function. And if it is some random stuff
(because you have void as return type), your OS might make wrong
assumptions and crash your app or the whole system.

hth
--
jb

(replace y with x to reply)



Sat, 04 Dec 2004 19:13:50 GMT  
 void main() or int main()?

Quote:
> I've been a C/C++ user for 2-3 years now. Whenever i need to write
> small console/shell applications, i always declare main's type as
> void, ie "void main(int argc, char *argv[]). However i've noticed a
> quite some people on usenet insisting that "void main()" should be
> "int main()".

> I personally feel that there is no difference, and that its a matter
> of preference. (Unless,of course, u do want main to return
something).
> so whats the difference? does that C++ standard mandate anything
about
> main's return type? and whats the position of the c99 standard on
> this?

Both ANSI C & C++  require int main(). If you do not return any value,
a value indicating successful completion is returned (the equivallent
of return 0; ).

For example:

#include <iostream>

int main()
{
  std::cout<<<"Hello new world!\n";

Quote:
}

is an ANSI C++ conforming program.

#include <stdio.h>

int main()
{
 printf("Hello old world!\n");

Quote:
}

is an ANSI C99/C++ conforming program.

--
Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys



Sat, 04 Dec 2004 19:14:44 GMT  
 void main() or int main()?


Quote:

> on comp.lang.c:
> > I've been a C/C++ user for 2-3 years now. Whenever i need to write
> > small console/shell applications, i always declare main's type as
> > void, ie "void main(int argc, char *argv[]). However i've noticed
a
> > quite some people on usenet insisting that "void main()" should be
> > "int main()".

> Those people are right. void main() has never been an allowed form
of
> the main function. The only possible return types for main() are int
> and implicit int.

    Implicit? I think I have read about something like that in
previous posts (i.e. "main () {}" would be allowed). Correct me if I'm
wrong, but afaik, C99 does not allow this anymore - and C++ does not
either.

regards
--
jb

(replace y with x to reply)



Sat, 04 Dec 2004 19:16:02 GMT  
 void main() or int main()?


Quote:
> I've been a C/C++ user for 2-3 years now. Whenever i need to write
> small console/shell applications, i always declare main's type as
> void, ie "void main(int argc, char *argv[]). However i've noticed a
> quite some people on usenet insisting that "void main()" should be
> "int main()".

> I personally feel that there is no difference, and that its a matter
> of preference. (Unless,of course, u do want main to return
something).
> so whats the difference? does that C++ standard mandate anything
about
> main's return type? and whats the position of the c99 standard on
> this?

    Here's also a link I dug up ... you should read it :)

    http://www.comeaucomputing.com/techtalk/#voidmain

hth
--
jb

(replace y with x to reply)



Sat, 04 Dec 2004 19:20:55 GMT  
 void main() or int main()?


Quote:


> >     Implicit? I think I have read about something like that in
> > previous posts (i.e. "main () {}" would be allowed). Correct me if
> I'm
> > wrong, but afaik, C99 does not allow this anymore - and C++ does
not
> > either.

> Yes they do not. However they allow:

> int main() {}

which is equivallent to:

int main() { return 0; }

--
Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys



Sat, 04 Dec 2004 19:31:42 GMT  
 void main() or int main()?

Quote:

>     Implicit? I think I have read about something like that in
> previous posts (i.e. "main () {}" would be allowed). Correct me if
I'm
> wrong, but afaik, C99 does not allow this anymore - and C++ does not
> either.

Yes they do not. However they allow:

int main() {}

--
Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys



Sat, 04 Dec 2004 19:30:28 GMT  
 void main() or int main()?
in comp.lang.c i read:

Quote:
>I've been a C/C++ user for 2-3 years now. Whenever i need to write
>small console/shell applications, i always declare main's type as
>void, ie "void main(int argc, char *argv[]). However i've noticed a
>quite some people on usenet insisting that "void main()" should be
>"int main()".

ah.  this month's void main troll.

doesn't anyone using google use the {*filter*}ing search function?

--
bringing you boring signatures for 17 years



Sat, 04 Dec 2004 20:14:53 GMT  
 void main() or int main()?

Quote:
> The difference is that int main() is standard and void main() is not.
> The C89, C90 and C99 standards are identical on this.

They are not. C99 has a small wrinkle:

Here is the relevant section:

5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation
declares no
prototype for this function. It shall be defined with a return type of int
and with no
parameters:

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

or with two parameters (referred to here as argc and argv, though any names
may be
used, as they are local to the function in which they are declared):

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

or equivalent;9) or in some other implementation-defined manner.

Notice the last 7 words of the paragraph. If a compiler vendor said our
implementation supports

void main(char *argv[]);

then C99 sanctions that. Personally I wish those last 7 words were not in
ISO C99, I think it is a mistake, but they are.

Stephen Howe



Sat, 04 Dec 2004 20:24:32 GMT  
 void main() or int main()?

Quote:


> on comp.lang.c:
> > I've been a C/C++ user for 2-3 years now. Whenever i need to write
> > small console/shell applications, i always declare main's type as
> > void, ie "void main(int argc, char *argv[]). However i've noticed a
> > quite some people on usenet insisting that "void main()" should be
> > "int main()".

> Those people are right. void main() has never been an allowed form of
> the main function. The only possible return types for main() are int
> and implicit int.

Implicit int isn't legal in C or C++ anymore.


Sat, 04 Dec 2004 21:26:51 GMT  
 void main() or int main()?

Quote:
>I've been a C/C++ user for 2-3 years now. Whenever i need to write
>small console/shell applications, i always declare main's type as
>void, ie "void main(int argc, char *argv[]). However i've noticed a
>quite some people on usenet insisting that "void main()" should be
>"int main()".

>I personally feel that there is no difference, and that its a matter
>of preference.

You didn't put much thought into the issue, did you?

Do you have *any* control over the code that is calling your main
function?  If not, why do you feel that matching the expectations of that
code about main's definition is *optional*?  The calling sequence of a
void function need not be compatible with the calling sequence of an int
function and, if they aren't, your program may crash before executing
its first statement.

Furthermore, there are implementations that generate an error message
when they see a misdefined main and don't generate any object code
(e.g. DEC C for OpenVMS).  Even gcc warns about void main.

Quote:
>(Unless,of course, u do want main to return something).
>so whats the difference? does that C++ standard mandate anything about
>main's return type? and whats the position of the c99 standard on
>this?

C has required main to return int ever since K&R1 was published, 24 years
ago.  C++ has required main to return int ever since Stroustrup's book
was first published (in 1985 or so).  Your personal "feeling" has
zilch support in the definitions of these programming languages.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sat, 04 Dec 2004 21:17:32 GMT  
 void main() or int main()?

Quote:

> I've been a C/C++ user for 2-3 years now. Whenever i need to write
> small console/shell applications, i always declare main's type as
> void, ie "void main(int argc, char *argv[]). However i've noticed a
> quite some people on usenet insisting that "void main()" should be
> "int main()".

> I personally feel that there is no difference, and that its a matter
> of preference. (Unless,of course, u do want main to return something).
> so whats the difference? does that C++ standard mandate anything about
> main's return type? and whats the position of the c99 standard on
> this?

Don't be a void mainer, int your main!

--
Thomas Stegen
http://www.geocities.com/thinkoidz



Sat, 04 Dec 2004 23:07:54 GMT  
 void main() or int main()?

Quote:

> I've been a C/C++ user for 2-3 years now. Whenever i need to write
> small console/shell applications, i always declare main's type as
> void, ie "void main(int argc, char *argv[]). However i've noticed a
> quite some people on usenet insisting that "void main()" should be
> "int main()".

> I personally feel that there is no difference, and that its a matter
> of preference. (Unless,of course, u do want main to return something).
> so whats the difference? does that C++ standard mandate anything about
> main's return type? and whats the position of the c99 standard on
> this?

> ---
> Black holes result when God
> divides the universe by zero
> ---

Hi deathstar

main is a special beast. In a conforming C++ program, only the int
main(<parameters>) is allowed. You do not have to return a value from
that function, however. If you do not specify a return statement, the
effect will be as if a "return 0" is executed.
I'm not familiar with C99, but i would be very surprised if main was
allowed to be defined as void.

Kind regards
Peter



Sat, 04 Dec 2004 22:22:19 GMT  
 void main() or int main()?

Quote:

> Don't be a void mainer, int your main!

Never use void where prohibitited by law.


Sat, 04 Dec 2004 22:29:55 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. void main or int main?

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

3. main() vs int main() vs int main(void)

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

5. newbie question....int main, void main, main

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

7. int main() vs int main(void)

8. int main vs. void main

9. int main() OR void main()...

10. void main() <-> int main()

11. int main() or void main()

12. void main() v/s int main()

 

 
Powered by phpBB® Forum Software