Legality of void main in C

Discussion in 'C Programming' started by prasoonthegreat@gmail.com, Jun 9, 2009.

  1. Guest

    Is void main legal in C....I know according to the standards it is
    illegal in C++....

    But after reading the standards.....

    The C Standard currently says that an implementation is free to define
    any additional forms for main that it cares to. § 5.1.2.2.1.1 of the C
    Standard lists the allowable forms for the definition of main. Because
    of the semi-colon, its final sentence parses as follows:

    It shall be defined

    * with a return type of int and
    o with no parameters [...] or
    o with two parameters [...] or equivalent;
    or
    * in some other implementation-defined manner.

    (Furthermore, § 5.1.2.2.3.1 says, of main,

    If the return type is not compatible with int, the termination
    status returned to the host environment is unspecified.

    which indicates that allowing forms that do not return int is
    intentional.) .......


    So can it(void main) be considered legal in C or not.....It is known
    to me that using void main is dangerous as it leads to process stack
    corruption sometimes .....

    So please focus on its legality!!!!!

    Prasoon
    , Jun 9, 2009
    #1
    1. Advertising

  2. writes:
    > Is void main legal in C....I know according to the standards it is
    > illegal in C++....


    The C standard doesn't use the word "legal" or "illegal" in that
    sense.

    > But after reading the standards.....
    >
    > The C Standard currently says that an implementation is free to define
    > any additional forms for main that it cares to. § 5.1.2.2.1.1 of the C
    > Standard lists the allowable forms for the definition of main. Because
    > of the semi-colon, its final sentence parses as follows:
    >
    > It shall be defined
    >
    > * with a return type of int and
    > o with no parameters [...] or
    > o with two parameters [...] or equivalent;
    > or
    > * in some other implementation-defined manner.
    >
    > (Furthermore, § 5.1.2.2.3.1 says, of main,
    >
    > If the return type is not compatible with int, the termination
    > status returned to the host environment is unspecified.
    >
    > which indicates that allowing forms that do not return int is
    > intentional.) .......


    Yes, an implementation is permitted to allow void main. A program
    that uses void main is valid for such an implementation; for other
    implementations, such a program's behavior is undefined, and the
    compiler is free to reject it outright.

    > So can it(void main) be considered legal in C or not.....It is known
    > to me that using void main is dangerous as it leads to process stack
    > corruption sometimes .....
    >
    > So please focus on its legality!!!!!


    You might as well assume that void main is "illegal"; there's no good
    reason to use it (except perhaps in a freestanding implementation).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 9, 2009
    #2
    1. Advertising

  3. arnuld Guest

    > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:

    >> writes:
    >> Is void main legal in C....I know according to the standards it is
    >> illegal in C++....


    > The C standard doesn't use the word "legal" or "illegal" in that
    > sense.



    Just want to know, if someone uses void main() what word I should use (if
    illegal is not the one).



    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
    arnuld, Jun 9, 2009
    #3
  4. James Kuyper Guest

    arnuld wrote:
    >> On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:

    >
    >>> writes:
    >>> Is void main legal in C....I know according to the standards it is
    >>> illegal in C++....

    >
    >> The C standard doesn't use the word "legal" or "illegal" in that
    >> sense.

    >
    >
    > Just want to know, if someone uses void main() what word I should use (if
    > illegal is not the one).


    In general, there's no single word that can be substituted for
    "illegal". You generally need multiple words, and you need different
    words in different contexts, and you might need to reorganize other
    parts of your sentence to make those words fit in. In most cases where
    you would want to say (incorrectly) that a piece of C codes "is
    illegal", the more accurate wording is one or more of the following:

    "is a constraint violation"
    "is a syntax error"
    "exceeds a minimum implementation limit"
    "has undefined behavior"

    In some cases, whether or not one of the above phrases applies depends
    upon unspecified behavior, in which case you need to prefix your
    statement with something like "it is implementation-specific whether
    ....". If the implementation is required to document which choice it
    makes for unspecified behavior, you can replace "specific" with "defined".

    In this particular case, you would say "It is implementation-defined
    whether or not void main() is supported. If it is not, use of void
    main() has undefined behavior".

    A more general statement, that covers all of the above, is
    "non-portable". A fully conforming compiler is free to define behavior
    that the C standard leaves undefined, to set its own implementation
    limits higher than the minimum levels, and to accept code that contains
    syntax errors or constraint violations. Therefore, whether or not a
    given piece of code will cause you trouble always depends upon which
    conforming compiler you use.

    Therefore, any language-specific (as opposed to algorithmic) problem
    with C code, no matter how severe, is technically merely a portability
    issue. However, "non-portable" means different things to different
    people, so it's better to be more specific, if you can be.
    James Kuyper, Jun 9, 2009
    #4
  5. arnuld <> writes:
    >> On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:

    >
    >>> writes:
    >>> Is void main legal in C....I know according to the standards it is
    >>> illegal in C++....

    >
    >> The C standard doesn't use the word "legal" or "illegal" in that
    >> sense.

    >
    >
    > Just want to know, if someone uses void main() what word I should use (if
    > illegal is not the one).


    Schildt.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 9, 2009
    #5
  6. Guest

    On Jun 9, 9:06 pm, Keith Thompson <> wrote:
    > arnuld <> writes:
    > >> On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:

    >
    > >>> writes:
    > >>> Is void main legal in C....I know according to the standards it is
    > >>> illegal in C++....

    >
    > >> The C standard doesn't use the word "legal" or "illegal" in that
    > >> sense.

    >
    > > Just want to know, if someone uses void main() what word I should use (if
    > > illegal is not the one).

    >
    > Schildt.
    >
    > --
    > Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
    > Nokia
    > "We must do something.  This is something.  Therefore, we must do this."
    >     -- Antony Jay and Jonathan Lynn, "Yes Minister"


    Thanks!!!!
    , Jun 9, 2009
    #6
  7. REH Guest

    On Jun 9, 12:07 pm, wrote:
    > On Jun 9, 9:06 pm, Keith Thompson <> wrote:
    > > arnuld <> writes:
    > > > Just want to know, if someone uses void main() what word I should use (if
    > > > illegal is not the one).

    >
    > > Schildt.

    >
    > Thanks!!!!


    That was a joke.

    REH
    REH, Jun 9, 2009
    #7
  8. Richard Bos Guest

    arnuld <> wrote:

    > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
    > > writes:


    > >> Is void main legal in C....I know according to the standards it is
    > >> illegal in C++....

    >
    > > The C standard doesn't use the word "legal" or "illegal" in that
    > > sense.

    >
    > Just want to know, if someone uses void main() what word I should use (if
    > illegal is not the one).


    "Stupid" would suffice, IMO. Outside of freestanding implementations,
    there is no good reason to use void main().

    Richard
    Richard Bos, Jun 9, 2009
    #8
  9. WangoTango Guest

    In article <4all.nl>, says...
    > arnuld <> wrote:
    >
    > > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
    > > > writes:

    >
    > > >> Is void main legal in C....I know according to the standards it is
    > > >> illegal in C++....

    > >
    > > > The C standard doesn't use the word "legal" or "illegal" in that
    > > > sense.

    > >
    > > Just want to know, if someone uses void main() what word I should use (if
    > > illegal is not the one).

    >
    > "Stupid" would suffice, IMO. Outside of freestanding implementations,
    > there is no good reason to use void main().
    >
    > Richard
    >

    OK, I'm confused.
    First you say it is 'stupid' and then give an example of where it would
    be applicable.
    I have written many apps that NEVER return to the environment that
    called them and, if they do, then any values that may be returned would
    be meaningless.
    Therefore, "void main(void)" is very applicable.

    The main function takes to arguments and returns to meaningful data to
    the calling environment, simple enough.
    WangoTango, Jun 9, 2009
    #9
  10. Lew Pitcher Guest

    On June 9, 2009 13:21, in comp.lang.c, WangoTango ()
    wrote:

    > In article <4all.nl>, says...
    >> arnuld <> wrote:
    >>
    >> > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
    >> > > writes:

    >>
    >> > >> Is void main legal in C....I know according to the standards it is
    >> > >> illegal in C++....
    >> >
    >> > > The C standard doesn't use the word "legal" or "illegal" in that
    >> > > sense.
    >> >
    >> > Just want to know, if someone uses void main() what word I should use
    >> > (if illegal is not the one).

    >>
    >> "Stupid" would suffice, IMO. Outside of freestanding implementations,
    >> there is no good reason to use void main().
    >>
    >> Richard
    >>

    > OK, I'm confused.
    > First you say it is 'stupid' and then give an example of where it would
    > be applicable.
    > I have written many apps that NEVER return to the environment that
    > called them and, if they do, then any values that may be returned would
    > be meaningless.


    So what? The C main() function is /defined by the language/ as returning an
    integer value; the /language/ says nothing about whether or not the
    execution environment (that which /calls main()/) will use or even
    recognize the return value.


    > Therefore, "void main(void)" is very applicable.


    No. I suggest that
    int main(void)
    {
    /* do something */
    return 0;
    }
    is applicable here.

    OTOH, while
    void main(void)
    {
    /* do something */
    }
    may work for you, it does so at risk of unexpected failure, /and/ violates
    the definition (to the system) of the main() function.

    > The main function takes to arguments


    sometimes. Sometimes not.

    > and returns to meaningful data to
    > the calling environment,


    which void main(void) cannot do

    > simple enough.


    You seem conflicted. You say that void main() is applicable, and then imply
    that it is not.

    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------
    Lew Pitcher, Jun 9, 2009
    #10
  11. WangoTango <> writes:
    > In article <4all.nl>, says...
    >> arnuld <> wrote:
    >> > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
    >> > > writes:
    >> > >> Is void main legal in C....I know according to the standards it is
    >> > >> illegal in C++....
    >> >
    >> > > The C standard doesn't use the word "legal" or "illegal" in that
    >> > > sense.
    >> >
    >> > Just want to know, if someone uses void main() what word I should use (if
    >> > illegal is not the one).

    >>
    >> "Stupid" would suffice, IMO. Outside of freestanding implementations,
    >> there is no good reason to use void main().
    >>
    >> Richard
    >>

    > OK, I'm confused.
    > First you say it is 'stupid' and then give an example of where it would
    > be applicable.
    > I have written many apps that NEVER return to the environment that
    > called them and, if they do, then any values that may be returned would
    > be meaningless.
    > Therefore, "void main(void)" is very applicable.
    >
    > The main function takes to arguments and returns to meaningful data to
    > the calling environment, simple enough.


    You mean "no arguments" and "no meaningful data", right?

    For a freestanding implementation, "void main(void)" may be valid, or
    even necessary. A freestanding implementation isn't even required to
    support the two forms of main() listed in the standard.

    For a hosted implementation, using "void main(void)" is -- well, I
    won't use the word "stupid", but it is foolish. If you don't need to
    return a meaningful value to the environment, you can just return 0
    (or, in C99, fall off the end of main() with the same result). If you
    declare "void main(void)", the compiler is free to reject your
    program, or to make it behave in some unpredictable manner. What
    benefit makes that worthwhile?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 9, 2009
    #11
  12. WangoTango Guest

    In article <1e4cb$4a2e9d35$4c0aa020$-Free>,
    says...
    > On June 9, 2009 13:21, in comp.lang.c, WangoTango ()
    > wrote:
    >
    > > In article <4all.nl>, says...
    > >> arnuld <> wrote:
    > >>
    > >> > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
    > >> > > writes:
    > >>
    > >> > >> Is void main legal in C....I know according to the standards it is
    > >> > >> illegal in C++....
    > >> >
    > >> > > The C standard doesn't use the word "legal" or "illegal" in that
    > >> > > sense.
    > >> >
    > >> > Just want to know, if someone uses void main() what word I should use
    > >> > (if illegal is not the one).
    > >>
    > >> "Stupid" would suffice, IMO. Outside of freestanding implementations,
    > >> there is no good reason to use void main().
    > >>
    > >> Richard
    > >>

    > > OK, I'm confused.
    > > First you say it is 'stupid' and then give an example of where it would
    > > be applicable.
    > > I have written many apps that NEVER return to the environment that
    > > called them and, if they do, then any values that may be returned would
    > > be meaningless.

    >
    > So what? The C main() function is /defined by the language/ as returning an
    > integer value; the /language/ says nothing about whether or not the
    > execution environment (that which /calls main()/) will use or even
    > recognize the return value.
    >
    >
    > > Therefore, "void main(void)" is very applicable.

    >
    > No. I suggest that
    > int main(void)
    > {
    > /* do something */
    > return 0;
    > }
    > is applicable here.
    >
    > OTOH, while
    > void main(void)
    > {
    > /* do something */
    > }


    more like
    void main(void)
    {
    while(TRUE)
    {

    }
    }

    > may work for you, it does so at risk of unexpected failure, /and/ violates
    > the definition (to the system) of the main() function.

    Always works for me.

    >
    > > The main function takes to arguments

    >
    > sometimes. Sometimes not.

    We are not talking ALWAYS.
    Depends on the environment.
    Not every 'C' program runs on a PC.

    >
    > > and returns to meaningful data to
    > > the calling environment,

    >
    > which void main(void) cannot do

    EXACTLY

    >
    > > simple enough.

    >
    > You seem conflicted. You say that void main() is applicable, and then imply
    > that it is not.


    I am not conflicted in the least.
    WangoTango, Jun 9, 2009
    #12
  13. WangoTango Guest

    In article <>, says...
    > WangoTango <> writes:
    > > In article <4all.nl>, says...
    > >> arnuld <> wrote:
    > >> > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
    > >> > > writes:
    > >> > >> Is void main legal in C....I know according to the standards it is
    > >> > >> illegal in C++....
    > >> >
    > >> > > The C standard doesn't use the word "legal" or "illegal" in that
    > >> > > sense.
    > >> >
    > >> > Just want to know, if someone uses void main() what word I should use (if
    > >> > illegal is not the one).
    > >>
    > >> "Stupid" would suffice, IMO. Outside of freestanding implementations,
    > >> there is no good reason to use void main().
    > >>
    > >> Richard
    > >>

    > > OK, I'm confused.
    > > First you say it is 'stupid' and then give an example of where it would
    > > be applicable.
    > > I have written many apps that NEVER return to the environment that
    > > called them and, if they do, then any values that may be returned would
    > > be meaningless.
    > > Therefore, "void main(void)" is very applicable.
    > >
    > > The main function takes to arguments and returns to meaningful data to
    > > the calling environment, simple enough.

    >
    > You mean "no arguments" and "no meaningful data", right?

    Yep, don't know how the noggin pulled that one off.
    Getting old is hell.

    >
    > For a freestanding implementation, "void main(void)" may be valid, or
    > even necessary. A freestanding implementation isn't even required to
    > support the two forms of main() listed in the standard.

    Exactly.

    >
    > For a hosted implementation, using "void main(void)" is -- well, I
    > won't use the word "stupid", but it is foolish. If you don't need to
    > return a meaningful value to the environment, you can just return 0
    > (or, in C99, fall off the end of main() with the same result). If you
    > declare "void main(void)", the compiler is free to reject your
    > program, or to make it behave in some unpredictable manner. What
    > benefit makes that worthwhile?

    I agree.
    I use the form "main(void)" with all of my current cross compilers, but
    some of my older implementations insisted on "void main(void)" because
    main never returns. Any attempt to fall off the bottom of the function
    is just a jump to the beginning.
    Anyway, I was just making the point that 'C' is run on a LOT of
    platforms and not all of them return to any type of operating system.
    WangoTango, Jun 9, 2009
    #13
  14. WangoTango <> writes:
    > In article <1e4cb$4a2e9d35$4c0aa020$-Free>,
    > says...
    >> On June 9, 2009 13:21, in comp.lang.c, WangoTango ()
    >> wrote:

    [...]
    >> > Therefore, "void main(void)" is very applicable.

    >>
    >> No. I suggest that
    >> int main(void)
    >> {
    >> /* do something */
    >> return 0;
    >> }
    >> is applicable here.
    >>
    >> OTOH, while
    >> void main(void)
    >> {
    >> /* do something */
    >> }

    >
    > more like
    > void main(void)
    > {
    > while(TRUE)
    > {
    >
    > }
    > }
    >
    >> may work for you, it does so at risk of unexpected failure, /and/ violates
    >> the definition (to the system) of the main() function.

    > Always works for me.


    So far.

    If you write:

    void main(void) { while (1) { /* ... */ } }

    then it's fairly likely to work on whatever system you're using, but
    it might fail. Failure can take many forms, obvious or subtle. The
    compiler can reject your program, your stack layouts can be messed up
    because your code didn't allocate space for the return value, and so
    forth. Failure can occur when you or somebody else ports the code to
    a new system, to a new compiler on the same system, to a new version
    of the same compiler, to the same compiler with different command-line
    options, or just because it's Tuesday. If your program malfunctions
    (and you happen to notice), and you complain to your compiler vendor,
    the vendor can legimimately tell you that your code is invalid and
    reject your bug report.

    If you write:

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

    then it *will* work on any conforming hosted implementation. None of
    the above problems apply. Oh, and it's one less letter to type.

    Can you explain again why void main is better?

    None of this applies if you're using a freestanding implementation
    that documents "void main(void)" as a valid entry point declaration.
    If so, you could have saved us a lot of time by mentioning it.

    [...]

    >> You seem conflicted. You say that void main() is applicable, and then imply
    >> that it is not.

    >
    > I am not conflicted in the least.


    You should be.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 9, 2009
    #14
  15. WangoTango <> writes:
    > In article <>, says...

    [...]
    >> For a hosted implementation, using "void main(void)" is -- well, I
    >> won't use the word "stupid", but it is foolish. If you don't need to
    >> return a meaningful value to the environment, you can just return 0
    >> (or, in C99, fall off the end of main() with the same result). If you
    >> declare "void main(void)", the compiler is free to reject your
    >> program, or to make it behave in some unpredictable manner. What
    >> benefit makes that worthwhile?

    >
    > I agree.
    > I use the form "main(void)" with all of my current cross compilers, but
    > some of my older implementations insisted on "void main(void)" because
    > main never returns. Any attempt to fall off the bottom of the function
    > is just a jump to the beginning.
    > Anyway, I was just making the point that 'C' is run on a LOT of
    > platforms and not all of them return to any type of operating system.


    "main(void)" is obsolete; C99 dropped the implicit int feature. For
    compilers that support it, it's exactly equivalent to "int main(void)".

    Falling off the bottom of the function should terminate the program.
    Perhaps it's just being re-invoked?

    In any case, since you're using cross compilers, you're probably using
    freestanding implementations, in which case void main(void) is
    perfectly valid if the implementation documents it, and this
    conversation has been a waste of time that could have been avoided if
    you'd just mentioned that fact earlier.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 9, 2009
    #15
  16. Guest

    On Jun 9, 12:58 pm, WangoTango <> wrote:
    > In article <1e4cb$4a2e9d35$4c0aa020$-Free>,
    > says...
    >
    >
    >
    > > On June 9, 2009 13:21, in comp.lang.c, WangoTango ()
    > > wrote:

    >
    > > > In article <4all.nl>, says....
    > > >> arnuld <> wrote:

    >
    > > >> > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
    > > >> > > writes:

    >
    > > >> > >> Is void main legal in C....I know according to the standards it is
    > > >> > >> illegal in C++....

    >
    > > >> > > The C standard doesn't use the word "legal" or "illegal" in that
    > > >> > > sense.

    >
    > > >> > Just want to know, if someone uses void main() what word I should use
    > > >> > (if illegal is not the one).

    >
    > > >> "Stupid" would suffice, IMO. Outside of freestanding implementations,
    > > >> there is no good reason to use void main().

    >
    > > >> Richard

    >
    > > > OK, I'm confused.
    > > > First you say it is 'stupid' and then give an example of where it would
    > > > be applicable.
    > > > I have written many apps that NEVER return to the environment that
    > > > called them and, if they do, then any values that may be returned would
    > > > be meaningless.

    >
    > > So what? The C main() function is /defined by the language/ as returning an
    > > integer value; the /language/ says nothing about whether or not the
    > > execution environment (that which /calls main()/) will use or even
    > > recognize the return value.

    >
    > > > Therefore, "void main(void)" is very applicable.

    >
    > > No. I suggest that
    > >   int main(void)
    > >   {
    > >     /* do something */
    > >     return 0;
    > >   }
    > > is applicable here.

    >
    > > OTOH, while
    > >   void main(void)
    > >   {
    > >     /* do something */
    > >   }

    >
    > more like
    > void main(void)
    > {
    >   while(TRUE)
    >   {
    >
    >   }
    >
    > }
    > > may work for you, it does so at risk of unexpected failure, /and/ violates
    > > the definition (to the system) of the main() function.

    >
    > Always works for me.
    >
    >
    >
    > > > The main function takes to arguments

    >
    > > sometimes. Sometimes not.

    >
    > We are not talking ALWAYS.
    > Depends on the environment.
    > Not every 'C' program runs on a PC.


    You go, girl! This is SUCH a big issue with me. Some people just
    don't get it; not everything is a nail, and sometimes "int main" is
    just not the right tool for the job. This is an issue of colossal
    importance, and we're damn lucky to have you here to fuel the debate.
    My hat's off to you, sir!

    Next up, gay marriage...
    , Jun 10, 2009
    #16
  17. Jack Klein <> writes:
    > On Mon, 8 Jun 2009 19:32:49 -0700 (PDT),
    > wrote in comp.lang.c:
    >
    >> Is void main legal in C....

    >
    > Even more important, did you know that "...." is NOT LEGAL in English?


    <OT>Not in this context, but it can be used as an ellipsis followed by
    a period (or full stop if you prefer). But yes, some writers' mania
    for inappropriate ellipses is both annoying and difficult to
    explain.</OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 10, 2009
    #17
  18. WangoTango Guest

    In article <>, says...
    > WangoTango <> writes:
    > > In article <>, says...

    > [...]
    > >> For a hosted implementation, using "void main(void)" is -- well, I
    > >> won't use the word "stupid", but it is foolish. If you don't need to
    > >> return a meaningful value to the environment, you can just return 0
    > >> (or, in C99, fall off the end of main() with the same result). If you
    > >> declare "void main(void)", the compiler is free to reject your
    > >> program, or to make it behave in some unpredictable manner. What
    > >> benefit makes that worthwhile?

    > >
    > > I agree.
    > > I use the form "main(void)" with all of my current cross compilers, but
    > > some of my older implementations insisted on "void main(void)" because
    > > main never returns. Any attempt to fall off the bottom of the function
    > > is just a jump to the beginning.
    > > Anyway, I was just making the point that 'C' is run on a LOT of
    > > platforms and not all of them return to any type of operating system.

    >
    > "main(void)" is obsolete; C99 dropped the implicit int feature. For
    > compilers that support it, it's exactly equivalent to "int main(void)".
    >
    > Falling off the bottom of the function should terminate the program.
    > Perhaps it's just being re-invoked?

    Some do a jump back to the entry point of main one turns off the INTs
    and executes a SLEEP instruction that halts execution until an interrupt
    occurs, essentially halting execution until a reset.
    >
    > In any case, since you're using cross compilers, you're probably using
    > freestanding implementations, in which case void main(void) is
    > perfectly valid if the implementation documents it, and this
    > conversation has been a waste of time that could have been avoided if
    > you'd just mentioned that fact earlier.


    Sorry.....
    WangoTango, Jun 10, 2009
    #18
  19. Guest

    On 10 June, 03:28, wrote:
    > On Jun 9, 12:58 pm, WangoTango <> wrote:
    > > In article <1e4cb$4a2e9d35$4c0aa020$-Free>,
    > > says...
    > > > On June 9, 2009 13:21, in comp.lang.c, WangoTango ()
    > > > wrote:
    > > > > In article <4all.nl>, says...
    > > > >> arnuld <> wrote:
    > > > >> > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
    > > > >> > > writes:



    > > > >> > >> Is void main legal in C....I know according to the standards it is
    > > > >> > >> illegal in C++....


    just a general remark here. Why ask or discuss the question if you
    aren't
    interested in the answer? I'm not addressing the OP here.


    > > > >> > > The C standard doesn't use the word "legal" or "illegal" in that
    > > > >> > > sense.


    nor does the C++ standard


    > > > >> > Just want to know, if someone uses void main() what word I should use
    > > > >> > (if illegal is not the one).

    >
    > > > >> "Stupid" would suffice, IMO. Outside of freestanding implementations,
    > > > >> there is no good reason to use void main().

    >
    > > > > OK, I'm confused.
    > > > > First you say it is 'stupid' and then give an example of where it would
    > > > > be applicable.


    "freestanding" has a special technical meaning as far as the C
    standard
    is concerned. "Hosted" implementations must provide the entire C
    library
    and are entered by a correctly defined main() function. "Freestanding"
    implementations are only required to provide stdlib, stdef and float
    (they can provide more if they want) and are entered "in an
    implementation
    defined manner (which could be main). clc usually discusses hosted
    implementations unless stated otherwise.

    "freestanding" are usually embedded systems. Though jokers
    have said since windows programs are entered via WinMain() this
    means windows is a freestandinging impl.


    > > > > I have written many apps that NEVER return to the environment that
    > > > > called them and, if they do, then any values that may be returned would
    > > > > be meaningless.

    >
    > > > So what? The C main() function is /defined by the language/ as returning an
    > > > integer value; the /language/ says nothing about whether or not the
    > > > execution environment (that which /calls main()/) will use or even
    > > > recognize the return value.

    >
    > > > > Therefore, "void main(void)" is very applicable.

    >
    > > > No. I suggest that
    > > >   int main(void)
    > > >   {
    > > >     /* do something */
    > > >     return 0;
    > > >   }
    > > > is applicable here.

    >
    > > > OTOH, while
    > > >   void main(void)
    > > >   {
    > > >     /* do something */
    > > >   }

    >
    > > more like
    > > void main(void)
    > > {
    > >   while(TRUE)
    > >   {

    >
    > >   }

    >
    > > }
    > > > may work for you, it does so at risk of unexpected failure, /and/ violates
    > > > the definition (to the system) of the main() function.

    >
    > > Always works for me.

    >
    > > > > The main function takes [two?] arguments

    >
    > > > sometimes. Sometimes not.

    >
    > > We are not talking ALWAYS.
    > > Depends on the environment.
    > > Not every 'C' program runs on a PC.


    there are two definitions of main() that must be provided by the
    standard
    int main (void);
    int main (int, char**); /* or equivalent */

    an implementaion *may* provide others but if you use an alternative
    your program becomes unportable. Why do this for such a little thing?

    <snip>
    , Jun 10, 2009
    #19
  20. Richard Bos Guest

    WangoTango <> wrote:

    > In article <4all.nl>, says...
    > > arnuld <> wrote:
    > >
    > > > Just want to know, if someone uses void main() what word I should use (if
    > > > illegal is not the one).

    > >
    > > "Stupid" would suffice, IMO. Outside of freestanding implementations,
    > > there is no good reason to use void main().

    >
    > OK, I'm confused.
    > First you say it is 'stupid' and then give an example of where it would
    > be applicable.


    Nope. For a hosted implementation, using void main() is stupid. For a
    freestanding implementation, this entire discussion is of no import
    whatsoever, since the article under discussion (5.1.2.2.1) only applies
    to hosted implementations in the first place.

    > I have written many apps that NEVER return to the environment that
    > called them and, if they do, then any values that may be returned would
    > be meaningless.
    > Therefore, "void main(void)" is very applicable.


    And what _does_ that return to the OS? Randomness, that's what. Very
    nice in a makefile.

    (Oh, you were talking about situations where you don't have an OS to
    return to? Then you aren't in 5.1.2.2 at all, but in 5.1.2.1, so this
    whole thread is not about you from the very first post.)

    Richard
    Richard Bos, Jun 11, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    495
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    768
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    809
    S.Tobias
    Jul 22, 2005
  4. Replies:
    1
    Views:
    385
    Victor Bazarov
    May 23, 2007
  5. Legality of void main in C

    , Jun 9, 2009, in forum: C Programming
    Replies:
    2
    Views:
    322
    Nate Eldredge
    Jun 10, 2009
Loading...

Share This Page