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


Quote:
>hi,
>i've seen some ppl saying:
>int main(void)
>is better than:
>void main(void)

It is because the standard says the first is  correct while the
other is not. Maybe it helps to think of a startuproutine of a real
implementation, which expects a function returning int which will
fail if the returnvalue is not.

h.f.s.

--
Hans Friedrich Steffani
Institut fuer Elektrische Maschinen und Antriebe, TU Chemnitz-Zwickau

http://www.*-*-*.com/ ~hfst/



Sat, 29 Jan 2000 03:00:00 GMT  
 void main(void) or int main() ??



Quote:
>hi,

>i've seen some ppl saying:

>int main(void)

>is better than:

>void main(void)

>now i was just wondering why that is..

>baaibaai

>    Floris

Entirely apart from the former being standard and the latter not,
consider an OS in which programs are expected to return an exit
code.  The profile "int main(void)" allows the program to return
an exit code via the return statement; the profile "void main(void)"
does not.

If we have a program, "alwaystrue", defined as:

    int main(void) { return 0; }

then a Bourne shell script that uses this program and the exit
code it returns:

    alwaystrue
    if [ $? -eq 0 ]; then echo True; else echo False; fi

will always echo "True".  But if we define the program as:

    void main(void) { }

then what the shell script will do is undefined.

(Strictly, the return value of main() is intercepted by the startup
code, which uses it for the exit code of the program.  If what main()
returns is undefined, then the program's exit code is also undefined,
and code that depends on that exit code will do unexpected things.)


Advanced Technology Center                   Phone (714) 583-9119
22982 Mill Creek Drive                                   ext. 220
Laguna Hills, CA 92653                       Fax   (714) 583-9213



Sat, 29 Jan 2000 03:00:00 GMT  
 void main(void) or int main() ??

Quote:



> >hi,
> >i've seen some ppl saying:
> >int main(void)
> >is better than:
> >void main(void)
> >now i was just wondering why that is..

Actually, not merely better but required by the standard.  void main()
produces undefined behavior and is not guarenteed to work in any
expected or predictable way on any platform.

Quote:
> Entirely apart from the former being standard and the latter not,
> consider an OS in which programs are expected to return an exit
> code.  The profile "int main(void)" allows the program to return
> an exit code via the return statement; the profile "void main(void)"
> does not.
> If we have a program, "alwaystrue", defined as:
>     int main(void) { return 0; }
> then a Bourne shell script that uses this program and the exit
> code it returns:
>     alwaystrue
>     if [ $? -eq 0 ]; then echo True; else echo False; fi

> will always echo "True".  But if we define the program as:
>     void main(void) { }
> then what the shell script will do is undefined.

If someone wrote a program using void main() (assuming it were actually
allowed) and someone else wrote a script that tried to interpret the
return code, we would blame the script writer not the programmer,
assuming, of course, that the lack of a meaningful return was
documented.  There are times when I write a program that simply does not
have any meaningful return value, for Unix I simply always return 0,
then document that the program invariably returns 0.  If some fool
script writer then does the above (reprinted here for clarification)

Quote:
>     alwaystrue
>     if [ $? -eq 0 ]; then echo True; else echo False; fi

or some similar construct that branches depending on the return value,
the script writer gets exactly what he/she deserves.

BTW, at one point I suggested that the void main() construct might be
used as a signal to the compiler to code all returns as return(0); and
all exits as exit(0), simply to save typing.  People here didn't like
that suggestion at all, so I retracted it.  It doesn't really save all
that much typing.  It is just as easy to do:

#define RET return(0)
#define EXT exit(0)

or something similar.

Quote:
> (Strictly, the return value of main() is intercepted by the startup
> code, which uses it for the exit code of the program.  If what main()
> returns is undefined, then the program's exit code is also undefined,
> and code that depends on that exit code will do unexpected things.)

One, if you write a program that has no meaningful exit code, presumably
you will specify that in the documentation so people won't write scripts
that try to interpret the exit code.  The idea that int main() is
required because most (or even all) operating systems seem to want a
return value is absurd.

The main() function is not truely a function belonging to the
programmer, it is the entry point for your program (or the re-entry
point).  main() is typically called by the start-up code, but it may
also be called or referenced from the Standard Library or any third
party extension library, as such, there needs to be a single method of
calling main.  Actually, if you define main as int main( void ) you
could still pass parameters to it (in fact argv[0] is 'always' passed,
as an empty string if nothing else), your program will simply ignore the
'unused' parameters.

Remember, The ANSI/ISO C Standard is independent of all
platforms/operating systems.  It is the responsiblilty of individual
implementations to insure that their compiler meets the needs of the
local platform/operating system.  Things like file systems, the
specifics of implementing stdin, sdtout, and stderr, or any other I/O
are left to the implementation.

--
*********************************************************

* Supporter of the campaign against grumpiness on c.l.c *
*********************************************************
*      Children,                                        *
*      We spend two years teaching them to talk...      *
*      and 20 years trying to get them to shut up!      *
*********************************************************
The money spent on an {*filter*},
as any woman may find out,
can better be spent on other things,
like taking the kids to an amusment park, or on vacation.



Sat, 29 Jan 2000 03:00:00 GMT  
 void main(void) or int main() ??

hi,

i've seen some ppl saying:

int main(void)

is better than:

void main(void)

now i was just wondering why that is..

baaibaai

        Floris



Sat, 29 Jan 2000 03:00:00 GMT  
 void main(void) or int main() ??


Quote:
> i've seen some ppl saying:
> int main(void)
> is better than:
> void main(void)
> now i was just wondering why that is..

int main() is guarenteed by the standard (the document that defines C)
as working.  void main() is something supported by some compilers.
While on those compilers it will probably work fine (apart from not
allowing you to select your return value - possibly leading to your
program reporting random results to the OS) on others it will not work
at all.  It may seem as though you're never going to need one of these
'strange' implementations, but since it takes so little effort to use
the standard, portable, int main() why not?

As the FAQ (avalibile from all good internet connections with ftp at
rtfm.mit.edu in pub/usenet/comp.lang.c/ and on the web at
http://www.eskimo.com/~scs/C-faq/top.html ) says:

11.12:  Can I declare main() as void, to shut off these annoying "main
        returns no value" messages?

A:      No.  main() must be declared as returning an int, and as taking
        either zero or two arguments, of the appropriate types.  If
        you're calling exit() but still getting warnings, you may have
        to insert a redundant return statement (or use some kind of "not
        reached" directive, if available).

        Declaring a function as void does not merely shut off or
        rearrange warnings: it may also result in a different function
        call/return sequence, incompatible with what the caller (in
        main's case, the C run-time startup code) expects.

        (Note that this discussion of main() pertains only to "hosted"
        implementations; none of it applies to "freestanding"
        implementations, which may not even have main().  However,
        freestanding implementations are comparatively rare, and if
        you're using one, you probably know it.  If you've never heard
        of the distinction, you're probably using a hosted
        implementation, and the above rules apply.)

11.14:  I believe that declaring void main() can't fail, since I'm
        calling exit() instead of returning, and anyway my operating
        system ignores a program's exit/return status.

A:      It doesn't matter whether main() returns or not, or whether
        anyone looks at the status; the problem is that when main() is
        misdeclared, its caller (the runtime startup code) may not even
        be able to *call* it correctly (due to the potential clash of
        calling conventions; see question 11.12).  Your operating system
        may ignore the exit status, and void main() may work for you,
        but it is not portable and not correct.

--

            http://www.geocities.com/SiliconValley/Lakes/7537/



Sun, 30 Jan 2000 03:00:00 GMT  
 void main(void) or int main() ??



Quote:
>hi,

>i've seen some ppl saying:

>int main(void)

>is better than:

>void main(void)

>now i was just wondering why that is..

Suppose that I write module containing a function

    char *foo(double x);

You also are writing a module which must call 'foo' inside my module.  Which
prototype would you prefer??

    a)   char *foo(double);
    b)   void foo(double);
    c)   char *foo(float);
    d)   none of the above

Similarly, your C implementation arranges for the environment to call a
start-up function in your C program. The C standard says that the function
should be defined either int main(int, char **) or int main(void). It mentions
no other choices but these two.  Is it reasonable to choose void main(void)?



Sun, 30 Jan 2000 03:00:00 GMT  
 void main(void) or int main() ??

Small embedded systems change the equation. If main returns a value
who gets to look at it. Most embedded systems never exit so there is
no need for main to return.  We recommend to our eight bit customers
to use void main (void) { .. }

Walter Banks

Quote:
> >i've seen some ppl saying:

> >int main(void)

> >is better than:

> >void main(void)

> >now i was just wondering why that is..



Tue, 01 Feb 2000 03:00:00 GMT  
 void main(void) or int main() ??

Quote:

> Small embedded systems change the equation. If main returns a value
> who gets to look at it. Most embedded systems never exit so there is
> no need for main to return.  We recommend to our eight bit customers
> to use void main (void) { .. }

That is fine as long as you tell them that it is not portable outside of
the embedded system.  
In any event a lot of newbies read comp.lang.c and it would be a very
poor idea to give them the impression that void main() was anything
other than a very poor idea that only works on a few specialized systems
and one 'bastard' system where almost anything goes.

--
*********************************************************

* Supporter of the campaign against grumpiness on c.l.c *
*********************************************************
* A Womans Guess is more accurate than a Mans certainty *
*                                       Rudyard Kipling *
*********************************************************
The money spent on an {*filter*},
as any woman may find out,
can better be spent on other things,
like taking the kids to an amusment park, or on vacation.



Tue, 01 Feb 2000 03:00:00 GMT  
 void main(void) or int main() ??



Quote:
>Small embedded systems change the equation. If main returns a value
>who gets to look at it.

Whatever called main() in the first place. For example the exit code could
be used to control behaviour, maybe selecting between a halt and a cold
or warm system reboot. And it doesn't matter if nothing looks at it, it
will still work fine.

Quote:
>Most embedded systems never exit so there is
>no need for main to return.

However that is a different issue to selecting the return type for main().
There is no requirement that a function returning void uses the same
machine code calling convention as one returning int - if you define
main() with the wrong type the system may not even succeed in calling it.
Defining main with the wrong type only results in undefined behaviour
as far as the language is concerned so an implementation can define as
an extensio support for other return types. However I've yet to see any
real justification for supporting a void return type.

Quote:
> We recommend to our eight bit customers
>to use void main (void) { .. }

What advantage does that have over int main(void) { } which is naturally
portable and well defined?

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


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



Thu, 03 Feb 2000 03:00:00 GMT  
 void main(void) or int main() ??

Groovy hepcat Floris v. Gog was jivin' on Tue, 12 Aug 1997 12:34:52
+0200 in comp.lang.c.
void main(void) or int main() ??'s a cool scene! Dig it!

Quote:
>int main(void)
>is better than:
>void main(void)

>now i was just wondering why that is..

  Because the ANSI/ISO C standard defines 2 ways that main() can be
defined. One is

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

and the other is

int main(void)

Anithing else results, acording to the standard, in undefined
behaviour, which means that anithing could happen. A particular
compiler may accept it, and everyting may be fine; but you shouldn't
rely on that. You should always use the standard way to be sure it
will work.
  The standard guarantees portability across platforms and
implimentations, too.

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Fri, 04 Feb 2000 03:00:00 GMT  
 void main(void) or int main() ??


Quote:

>Small embedded systems change the equation. If main returns a value
>who gets to look at it. Most embedded systems never exit so there is
>no need for main to return.  We recommend to our eight bit customers
>to use void main (void) { .. }

You are no longer talking about a hosted implementation of the C language. The
``equation'' does indeed change, and the standard contains a provision:

    5.1.2.1

        In a freestanding environment (in which C program execution may take
    place without any benefit of an operating system), the name and type of the
    function called at startup are implementation-defined.

Thus you are not incorrect to recommend void main(void), as long as that
is what the embedded C implementation requires.



Fri, 04 Feb 2000 03:00:00 GMT  
 void main(void) or int main() ??

Quote:



>>Most embedded systems never exit so there is
>>no need for main to return.

>However that is a different issue to selecting the return type for main().
>There is no requirement that a function returning void uses the same
>machine code calling convention as one returning int - if you define
>main() with the wrong type the system may not even succeed in calling it.
>Defining main with the wrong type only results in undefined behaviour

Walter however, is describing a freestanding implementation, and
as the compiler+library supplier he is in a position to ensure that
the calling convention is such that he can define when "void main()"
will work and is applicable.


Fri, 04 Feb 2000 03:00:00 GMT  
 void main(void) or int main() ??



Quote:



>>>Most embedded systems never exit so there is
>>>no need for main to return.

>>However that is a different issue to selecting the return type for main().
>>There is no requirement that a function returning void uses the same
>>machine code calling convention as one returning int - if you define
>>main() with the wrong type the system may not even succeed in calling it.
>>Defining main with the wrong type only results in undefined behaviour

>Walter however, is describing a freestanding implementation, and
>as the compiler+library supplier he is in a position to ensure that
>the calling convention is such that he can define when "void main()"
>will work and is applicable.

I agree he can do that. My real question though is whether there is any
good reason to do so.

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


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



Sun, 06 Feb 2000 03:00:00 GMT  
 
 [ 13 post ] 

 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. newbie question....int main, void main, main

4. int main() vs int main(void)

5. main(void) vs void main ()

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() <-> int main()

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

12. int main() or void main()

 

 
Powered by phpBB® Forum Software