void main() <-> int main() 
Author Message
 void main() <-> int main()



Quote:
> Hello,

> From another posting I got the reply that
> void main()
> isnt' correct but it should be int main()

Yep, that's right.

Quote:

> I've always used void main() in my progrmans not knowing it was wrong,
could
> this have an effect in my programs ?

Yes, it could. It probably won't, whilst you continue to use Microsoft C
(did I guess correctly?), but there is no guarantee - as far as I know,
Microsoft have not formally documented void main as an extension. But since
it appears in about 80%* of their help doc programs, perhaps you might be
lucky (possibly) as long as you don't change or upgrade your compiler or
your machine - ever.

Strictly speaking, void main() programs exhibit undefined behaviour.

Quote:
> I never noticed something was wrong.

Undefined behaviour can of course include whatever behaviour you were
expecting. It can also include random formatting of disk sectors, or demons
coming out of your nose.

Quote:

> What int value does the main return ?

There are three portable values you may return from main(): 0,
EXIT_SUCCESS, and EXIT_FAILURE. The last-named two values are provided by
<stdlib.h> - they're given names because their values are
implementation-dependent. (BTW the same applies to the exit() function -
these are the only portable values you may send to it.)

If you've never bothered returning anything from main() before, just use 0
for all your programs.  :-)

#include <stdio.h>

int main(void)
{
  printf("Hello, ANSI C world!\n");
  return 0;

Quote:
}

--
Richard Heathfield

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.



Thu, 07 Feb 2002 03:00:00 GMT  
 void main() <-> int main()
On Mon, 23 Aug 1999 00:01:03 +0200, "Nico Lugil"

Quote:
> Hello,

> From another posting I got the reply that
> void main()
> isnt' correct but it should be int main()

> I've always used void main() in my progrmans not knowing it was wrong, could
> this have an effect in my programs ?
> I never noticed something was wrong.

See http://home.att.net/~jackklein/ctips01.html#int_main.

Quote:
> What int value does the main return ?

See http://home.att.net/~jackklein/ctips01.html#return.

Quote:
> Thanks

> Nico

Jack Klein
--
Home: http://home.att.net/~jackklein


Thu, 07 Feb 2002 03:00:00 GMT  
 void main() <-> int main()
On Mon, 23 Aug 1999 00:37:20 +0200, "Nico Lugil"

Quote:





> >> Hello,

> >> From another posting I got the reply that
> >> void main()
> >> isnt' correct but it should be int main()

> >Yep, that's right.

> >> I've always used void main() in my progrmans not knowing it was wrong,
> >could
> >> this have an effect in my programs ?

> >Yes, it could. It probably won't, whilst you continue to use Microsoft C
> >(did I guess correctly?), but there is no guarantee - as far as I know,
> >Microsoft have not formally documented void main as an extension. But since
> >it appears in about 80%* of their help doc programs, perhaps you might be
> >lucky (possibly) as long as you don't change or upgrade your compiler or
> >your machine - ever.

> Thanks for your answer, but I'm not using Microsoft C, but just the standard
> C
> compiler on a HPUX unix-station. Do you mean that Microsoft uses void main()
> in their help programs ???

Yes, in almost all of the example programs in their online help.

Jack Klein
--
Home: http://home.att.net/~jackklein



Thu, 07 Feb 2002 03:00:00 GMT  
 void main() <-> int main()

Quote:
>I've always used void main() in my progrmans not knowing it was wrong,
could
>this have an effect in my programs ?

Officially: yes, your program might delete your entire harddisk or mail your
girlfriends' pictures to tabloids.
On the average i would not expect to, but in the future their may be
compilers that will do some of the above things..

Quote:
>I never noticed something was wrong.
>What int value does the main return ?

Depending on what you let it return:

int main() {
return 0;

Quote:
}

SortaQuotefromK&R: "Return 0 in main indicates normal termination"

Greetings



Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()
Hello,

From another posting I got the reply that
void main()
isnt' correct but it should be int main()

I've always used void main() in my progrmans not knowing it was wrong, could
this have an effect in my programs ?
I never noticed something was wrong.

What int value does the main return ?

Thanks

Nico



Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()


Quote:


>> Hello,

>> From another posting I got the reply that
>> void main()
>> isnt' correct but it should be int main()

>Yep, that's right.

>> I've always used void main() in my progrmans not knowing it was wrong,
>could
>> this have an effect in my programs ?

>Yes, it could. It probably won't, whilst you continue to use Microsoft C
>(did I guess correctly?), but there is no guarantee - as far as I know,
>Microsoft have not formally documented void main as an extension. But since
>it appears in about 80%* of their help doc programs, perhaps you might be
>lucky (possibly) as long as you don't change or upgrade your compiler or
>your machine - ever.

Thanks for your answer, but I'm not using Microsoft C, but just the standard
C
compiler on a HPUX unix-station. Do you mean that Microsoft uses void main()
in their help programs ???


Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()


Quote:
> On Mon, 23 Aug 1999 00:37:20 +0200, "Nico Lugil"





> > >> I've always used void main() in my progrmans not knowing it was
wrong,
> > >could
> > >> this have an effect in my programs ?

> > >Yes, it could. It probably won't, whilst you continue to use Microsoft
C
> > >(did I guess correctly?), but there is no guarantee - as far as I
know,
> > >Microsoft have not formally documented void main as an extension. But
since
> > >it appears in about 80%* of their help doc programs, perhaps you might
be
> > >lucky (possibly) as long as you don't change or upgrade your compiler
or
> > >your machine - ever.

> > Thanks for your answer, but I'm not using Microsoft C, but just the
standard
> > C
> > compiler on a HPUX unix-station. Do you mean that Microsoft uses void
main()
> > in their help programs ???

> Yes, in almost all of the example programs in their online help.

On reading this, I noticed that I'd asterisked my 80% figure, but not
provided the matching footnote. Here it is:

*91.037% of statistics are made up on the spot.

But 80% is as good a guide as any, short of an actual count - which I doubt
anyone who reads comp.lang.c has the patience to perform.



Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()
On 22 Aug 1999 23:34:47 +0100, "Richard Heathfield"

[snip]

Quote:
>Yes, it could. It probably won't, whilst you continue to use Microsoft C
>(did I guess correctly?), but there is no guarantee - as far as I know,
>Microsoft have not formally documented void main as an extension. But since
>it appears in about 80%* of their help doc programs, perhaps you might be
>lucky (possibly) as long as you don't change or upgrade your compiler or
>your machine - ever.

I haven't found void main() documented in the C section of the MSVC++
documentation, but here it is in the Microsoft C++ Language Reference in
the MSVC++ Help (slightly off-topic here, but of interest, nonetheless).
----------------------------------------
...
The declaration syntax for main is:

int main( );

or, optionally:

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

The declaration syntax for wmain is as follows:

int wmain( );

or, optionally:

int wmain( int argc[ , wchar_t *argv[ ] [, wchar_t *envp[ ] ] ] );

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Alternatively, the main and wmain functions can be declared as returning
void (no return value). If you declare main or wmain as returning void, you
cannot return an exit code to the parent process or operating system using
a return statement; to return an exit code when main or wmain are declared
as void, you must use the exit function.
...
----------------------------------------
Note also the non-conforming definitions of int main() (without comment
that they are extensions)! Also no comment about legal return values.

For the original poster's benefit, here is how main should be defined in C
(C Language Draft Standard N869):

----------------------------------------
       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;8)  or in  some  other  implementation-defined
       manner.
...
----------------------------------------

Regards,
Bruce Wheeler



Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()

Quote:

>Hello,

>From another posting I got the reply that
>void main()
>isnt' correct but it should be int main()

>I've always used void main() in my progrmans not knowing it was wrong, could
>this have an effect in my programs ?

A call to an int(void) function might have use calling conventions
that do not match those expected by the void (void) definition.

For example, imagine an implementation of C which is stack based.
A return value is pushed onto the stack. Since the void function
doesn't produce the expected return value, the caller will trigger
a stack underflow.

This is both a matter of type and a matter of standards conformance.

Firstly, the ANSI/ISO C standard specifies two type signatures for main that
are mandatory for an implementation to support. Both of these have an int
return value.  If you rely on a different type signature, your program falls
outside ANSI C.  As an engineer, you want to have precise control and purpose
in introducing features into the software that cause it to go outside of an
applicable standard. Such things require some justification, like ``I need this
non-portable feature because it's not available otherwise''.

Secondly, the type system is not to be taken lightly. If a specification
says that you are to implement a function that returns int and takes
certain parameters, you implement it exactly.

For example, if you are writing a device driver and the spec says that you must
implement a function called open() which returns an int and takes a char *,
then by golly, you will write a function that returns an int and takes a char
*, right? Otherwise there is a type mismatch, possibly an undiagnosable one.
Type mismatches are a bad thing, even if they don't always result in
a physical mismatch.

Quote:
>I never noticed something was wrong.

>What int value does the main return ?

ANSI/ISO C specifies that by returning zero, or the value EXIT_SUCCESS defined
in <stdlib.h> the program indicates a successful termination.  Returning
EXIT_FAILURE is used to indicate a failed termination.


Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()
Kaz Kylheku a crit dans le message ...

Quote:

>For example, imagine an implementation of C which is stack based.
>A return value is pushed onto the stack. Since the void function
>doesn't produce the expected return value, the caller will trigger
>a stack underflow.

Yes but C is not Pascal !

But some "pascal" non standard (?) keyword word can inforce the pascal
calling convention for a C-function (Windows API by example) so you are
right, it may happen.

--
HS



Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()
On Mon, 23 Aug 1999 20:47:17 +0200, Homer Simpson

Quote:

>Kaz Kylheku a crit dans le message ...

>>For example, imagine an implementation of C which is stack based.
>>A return value is pushed onto the stack. Since the void function
>>doesn't produce the expected return value, the caller will trigger
>>a stack underflow.

>Yes but C is not Pascal !

>But some "pascal" non standard (?) keyword word can inforce the pascal
>calling convention for a C-function (Windows API by example) so you are
>right, it may happen.

Please, don't start another calling convention thread.

I've been reading your posts and I must say that I'm rather unimpressed.

I get the feeling that you really *are* Homer Simpson. Don't you have
a reactor meltdown to prevent?



Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()
Return int to void is ok
Return void to int is not
you have got lucky when you  used "void main(..)" because main is defined as
"int main(...)"
if it was defined as "void main(...)" and you wrote "int main(...)" your s/w
would not compiled
Quote:

> Hello,

> From another posting I got the reply that
> void main()
> isnt' correct but it should be int main()

> I've always used void main() in my progrmans not knowing it was wrong, could
> this have an effect in my programs ?
> I never noticed something was wrong.

> What int value does the main return ?

> Thanks

> Nico



Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()
Sorry
void --->to --> int  is ok
int ----->to--> void is not
Sorry again
Quote:

> Return int to void is ok
> Return void to int is not
> you have got lucky when you  used "void main(..)" because main is defined as
> "int main(...)"
> if it was defined as "void main(...)" and you wrote "int main(...)" your s/w
> would not compiled


> > Hello,

> > From another posting I got the reply that
> > void main()
> > isnt' correct but it should be int main()

> > I've always used void main() in my progrmans not knowing it was wrong, could
> > this have an effect in my programs ?
> > I never noticed something was wrong.

> > What int value does the main return ?

> > Thanks

> > Nico



Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()
On 23 Aug 1999 00:25:23 +0100, "Richard Heathfield"

Quote:

>On reading this, I noticed that I'd asterisked my 80% figure, but not
>provided the matching footnote. Here it is:

>*91.037% of statistics are made up on the spot.

>But 80% is as good a guide as any, short of an actual count - which I doubt
>anyone who reads comp.lang.c has the patience to perform.

Ahem.  Note the 15-year-old .sig

--
#include <standard.disclaimer>
 _
Kevin D Quitt  USA 91351-4454           96.37% of all statistics are made up
Per the FCA, this email address may not be added to any commercial mail list



Fri, 08 Feb 2002 03:00:00 GMT  
 void main() <-> int main()

: > Hello,
: >
: > From another posting I got the reply that
: > void main()
: > isnt' correct but it should be int main()
: >
: > I've always used void main() in my progrmans not knowing it was wrong, could
: > this have an effect in my programs ?
: > I never noticed something was wrong.
: >
: > What int value does the main return ?

Traditionally, if the program ended normally and successfully, main()
returns 0.  Anything else is considered an error code.  Does anyone know
if this is only a Unix usage, or carried over to other (not fun) operating
systems?

--
---------------------------------------------------------------

Remove spam, eggs, bacon, spam, and dot to reply.

LUTHER!



Fri, 08 Feb 2002 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. main() vs int main() vs int main(void)

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

3. newbie question....int main, void main, main

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

5. int main() vs int main(void)

6. int main vs. void main

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

8. void main or int main?

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

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

11. int main() or void main()

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

 

 
Powered by phpBB® Forum Software