Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: C main() function

  1. #1
    Join Date
    Feb 2009
    Location
    Villa Rica, GA
    Beans
    Hidden!

    C main() function

    Hi all,

    In another thread, nvteighen said the main() function should always be

    Code:
    int main(void)
    or
    Code:
    int main(int argc, char *argv[])
    ,
    never
    Code:
    int main()
    What is the reasoning behind this? Is it really such a big deal? I've always used int main() with no problems, since I started with C++ and the habit just sort of continued.
    One does not simply rock into Mordor.

  2. #2
    Join Date
    Jun 2007
    Location
    Paraparaumu, New Zealand
    Beans
    Hidden!

    Re: C main() function

    Your first example lets your program return an "exit value" (MS-DOS/Windows: "Errorlevel"), but doesn't use the command-line arguments. Your second example adds in the use of the command-line arguments.

    "int main()" is a little bit "out there", neither saying that you're using command-line arguments, nor saying that you're not using them. The first two examples are "more standard".
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  3. #3
    Join Date
    Feb 2009
    Location
    Villa Rica, GA
    Beans
    Hidden!

    Re: C main() function

    Ah. From now on I will try to remember to put the 'void' in there, I would hate to confuse my computer!

    Thanks!
    One does not simply rock into Mordor.

  4. #4
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: C main() function

    the reason main() is worse than main(void) has to do with portability
    some compilers need(ed?) the void if a function (and not only main) had no arguments
    I think it had to to with K&R C but I can't remember the exact reasoning anymore

    there's even a special warning for that in gcc: -Wold-style-definition

  5. #5
    Join Date
    Feb 2009
    Beans
    103

    Re: C main() function

    In the most popular variant of C, C89, 'int main()' is completely allowed, and is the most used. This also applies to K&R C and C++. In fact, in C89 and K&R C, you can even leave out the 'int' part, as it is implied.

    But this is where it changes... 'int main()' in C99 shouldn't be used, instead it should be 'int main(void)'. I think Obj-C is using C99 (It was last time I compiled some Obj-C files), since it is a strict-subset, thus that requires 'int main(void)' as well.

    If you wish to be complacent with the latest C standard, use 'int main(void)'. If you wish it to be a subset of C++, the 'void' part is redundant.
    "The Map is Not the Territory" - Alfred Korzybski

  6. #6
    Join Date
    Dec 2006
    Beans
    256

    Re: C main() function

    It is helpful to recognize the distinction between declaring a function with "foo(void)" versus with "foo()". The former explicitly forbids any arguments from being passed to the function; the latter merely indicates that nothing is known about the arguments. A call with arguments would not necessarily fail, and is typically permitted by the compiler (though warnings may be generated).

    'main' is unique in the C language in that it can have either no or two arguments and still be compliant with the standard (ANSI). Some compilers allow more than two arguments (MS C has three, and others permit variable-length lists of arguments), but these are non-compliant. Leaving out 'void' is not equivalent to having either zero or two arguments, so it is likewise non-compliant.

    If your program is intended to be maximally portable, it should comply with the ANSI standard. This means using 'void' when no arguments are expected.
    "We visited sixty-six islands and landed eighty-one times, wading, swimming (to shore). Most of the people were friendly and delightful; only two arrows shot at us, and only one went near -- So much for savages!" - J.C. Patterson

  7. #7
    Join Date
    Aug 2007
    Location
    Manchester, UK
    Beans
    10,285
    Distro
    Ubuntu

    Re: C main() function

    Whenever I write any C applications now, I tend to use
    Code:
    int main(int argc, char *argv[])
    Just in case I want to take any command line arguments

  8. #8
    Join Date
    Oct 2005
    Location
    Seattle, WA
    Beans
    494
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C main() function

    Now I know in C, the arguments to a function are stored on the stack right after the function call, correct? So by declaring argc and argv, you are making space on the stack for them.

    Now I know void == nothing, but is there a difference in the way the stack is managed with using void versus not using it?

    Kinda out there, but curious if anyone knows off the top of their head
    www.runtime-era.com - Blog About My Journey as a Developer

  9. #9
    Join Date
    Aug 2006
    Location
    Madrid, Spain
    Beans
    299
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: C main() function

    Quote Originally Posted by era86 View Post
    Now I know void == nothing, but is there a difference in the way the stack is managed with using void versus not using it?
    In old C, functions were not always prototyped. Declaring a function as "T f();" tells the compiler that any arguments are acceptable (including no arguments at all). Acceptable calls would be f(), f(5), f("a", &var), etc. On the other hand, declaring it as "T f(void);" specifies that it takes no arguments, so any call other than f() is a compiler error. C99 (and common sense) advises to always declare a function that takes no arguments as "T f(void);"

    In C++, both declarations mean exactly the same, since the name manling required by its function overload feature makes it necessary for the compiler to know the full prototype of any function before calling it.

    WRT to your stack question, in the usual C/C++ calling convention, the caller is tasked with cleaning up the passed arguments from its stack after a function call returns. Thus, even if you declare a function with "T f();" and call it like f(5), that 5 will rest in the stack, unused by the function, and be removed by the caller when f returns. So no damage done, other than possible optimization opportinities lost by the compiler.
    May the Source be with you.

  10. #10
    Join Date
    Oct 2009
    Location
    Lincolnshire, UK
    Beans
    16

    Re: C main() function

    Although your original Q has been answered, you might find this link to the comp.lang.c FAQ useful for future reference (see answer 11.12a -- and links -- for discussion about your original question).

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •