int main() or int main(void)?

Discussion in 'C Programming' started by Frederick Ding, Dec 3, 2005.

  1. Hi, guys!
    I met a problem:
    Should I use "int main()" or "int main(void)", is that a kind of
    "style"
    problem?
    And which is the standard C99 recommend?
    If it's a "style" one, where can I get the standard or nearly
    standard C
    style? K&R or what?

    Thanks a lot!

    Frederick Ding
    2005-12-3
     
    Frederick Ding, Dec 3, 2005
    #1
    1. Advertising

  2. Frederick Ding said:

    > Hi, guys!
    > I met a problem:
    > Should I use "int main()" or "int main(void)", is that a kind of
    > "style"
    > problem?


    int main(void) is a full function prototype, and thus is preferable to a
    mere declaration.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 3, 2005
    #2
    1. Advertising

  3. Thanks!
    I does not mean the prototype, but full function:
    I think the main function is only used only once in a .c file, so
    it does not need
    a pre-declare or something like that.
    Am I right? Thanks for your help!
    Should I use:
    #include <stdio.h>
    int main()
    {
    ....
    return 0;
    }

    or this:
    #include <stdio.h>
    int main(void)
    {
    ....
    return 0;
    }


    Thanks!
     
    Frederick Ding, Dec 3, 2005
    #3
  4. I checked the K&R(2nd Edition) just now, and find that it is really
    obsolete, the function "main" in it only writes : main() , even without
    a return value type.
    Maybe I should drop the book and pickup a new one that conform to C99
    standard.
     
    Frederick Ding, Dec 3, 2005
    #4
  5. Frederick Ding said:

    > Thanks!
    > I does not mean the prototype, but full function:


    Then you don't understand what "function prototype" means. A function
    prototype is a function declaration that lists its parameters. A function
    definition is also a function declaration. A prototype allows the compiler
    to do type checking and argument counting (except, of course, in the case
    of variable argument lists). Consider this extremely simplified example of
    why function prototypes are important:

    int mani(int i)
    {
    return i / 2;
    }

    int main(void) /* <--- this is a prototype */
    {
    return main(1); /* mani(1) was intended. The compiler
    * can and must catch this error. */
    }

    Now consider this:

    int mani(int i)
    {
    return i / 2;
    }

    int main() /* <--- this is NOT a prototype */
    {
    return main(1); /* mani(1) was intended. The compiler
    * is not required to catch this error.
    * gcc certainly didn't when I tried it
    * with -W -Wall -ansi -pedantic */
    }


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 3, 2005
    #5
  6. Frederick Ding said:

    > I checked the K&R(2nd Edition) just now, and find that it is really
    > obsolete, the function "main" in it only writes : main() , even without
    > a return value type.


    Well, there's an implicit int there. Nevertheless, you are right - this is
    what I would consider to be poor style. K&R is not a perfect book. It is
    merely the very best C book available.

    > Maybe I should drop the book and pickup a new one that conform to C99
    > standard.


    You would be doing yourself a disservice. K&R remains far and away the best
    available book on C. It's high time someone wrote a better one, but they
    have not done so yet.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 3, 2005
    #6
  7. Thank you very much, now I see.
     
    Frederick Ding, Dec 3, 2005
    #7
  8. Frederick Ding

    pemo Guest

    "Frederick Ding" <> wrote in message
    news:...
    > Hi, guys!
    > I met a problem:
    > Should I use "int main()" or "int main(void)", is that a kind of
    > "style"
    > problem?
    > And which is the standard C99 recommend?
    > If it's a "style" one, where can I get the standard or nearly
    > standard C
    > style? K&R or what?


    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.

    C: A Reference Manual is a good book - as is K&R.

    Also, check out the K&R errata
    http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
     
    pemo, Dec 3, 2005
    #8
  9. Frederick Ding

    Malcolm Guest

    "Richard Heathfield" <> wrote
    > You would be doing yourself a disservice. K&R remains far and away the
    > best
    > available book on C. It's high time someone wrote a better one, but they
    > have not done so yet.
    >

    Someone did have a very good go with C Unleashed.
    It's more a second book on C than a primer, though.
     
    Malcolm, Dec 3, 2005
    #9
  10. Frederick Ding

    Chris Hills Guest

    In article <>,
    Frederick Ding <> writes
    >I checked the K&R(2nd Edition) just now, and find that it is really
    >obsolete, the function "main" in it only writes : main() , even without
    >a return value type.
    >Maybe I should drop the book and pickup a new one that conform to C99
    >standard.
    >


    It depends which compiler you are using. C99 compiler are few and far
    between.

    Return values should ALWAYS be used except in some self hosted systems
    where there is no OS to return to.

    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
     
    Chris Hills, Dec 3, 2005
    #10
  11. Frederick Ding

    pete Guest

    Frederick Ding wrote:
    >
    > I checked the K&R(2nd Edition) just now, and find that it is really
    > obsolete, the function "main" in it only writes : main() ,
    > even without
    > a return value type.
    > Maybe I should drop the book and pickup a new one that conform to C99
    > standard.


    At any rate, download the errata,
    http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html

    K&R 1, is older than void,
    and a lot of things like int main(void)
    didn't make it into K&R 2.

    If you have K&R2 and the errata, and clc and
    (N869 or a copy of one or both standards)
    you should be OK.

    --
    pete
     
    pete, Dec 4, 2005
    #11
    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. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  2. int main(void) Why void?

    , Apr 6, 2006, in forum: C Programming
    Replies:
    6
    Views:
    521
  3. Army1987

    int main(void) { return main(); }

    Army1987, Mar 29, 2007, in forum: C Programming
    Replies:
    37
    Views:
    1,484
    Daniel Rudy
    Apr 3, 2007
  4. Replies:
    55
    Views:
    1,505
    Lionel B
    Jan 16, 2008
  5. Tristan Wibberley

    Re: int main(void) is better than int main()

    Tristan Wibberley, Jan 4, 2008, in forum: C++
    Replies:
    1
    Views:
    356
    Lars Uffmann
    Jan 7, 2008
Loading...

Share This Page