int main ()?

This is a discussion on int main ()? within the C Programming forums, part of the General Programming Boards category; I'm new to programming, and I'm wondering why sometimes you see: int main (void), and other times you just see ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    int main ()?

    I'm new to programming, and I'm wondering why sometimes you see:

    int main (void), and other times you just see int main()? Why is it necessary to

    include something in the parentheses?

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    I was told that main() is the old style and might cause problems, but main(void) is newer and unambiguous. I don't know any details though.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,709
    From what I understand, in the case of the main function both are acceptable since no prototype is declared for main. The C99 standard presents the int main(void) version when describing the main function, but has a number of examples that use the int main() version.

    In the case of other functions, if the function is declared in the form foo(), it could take a variable number of arguments, but if it is declared in the form foo(void), it takes no arguments.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    When declaring a function, () means "unspecified arguments" and (void) means "no arguments". You should never write a function using just () because the compiler won't (necessarily) be able to tell you if you're using it incorrectly. Most code doesn't call main() so I guess it's acceptable to use (); but there's no reason to.. and anyway, it is legal to call main() so you may as well declare it as taking no arguments if that's what you mean:
    Code:
    int main() { main(1); } /* this requires no diagnostic */
    int main(void) { main(1); } /* this does */

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,709
    Quote Originally Posted by cas
    and anyway, it is legal to call main() so you may as well declare it as taking no arguments if that's what you mean
    On the other hand, calling main() is unlikely to be a good thing. More likely such code would be better written with a loop construct.

    Quote Originally Posted by Elysia
    In C, all functions take a variable amount of arguments, unless you explicitly put "void" in the argument list.
    I am not a C programmer, but from what I understand that is incorrect. To specify a variable number of arguments, one should either declare the function with an empty parameter list, or end the parameter list with an ellipsis.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cas View Post
    Most code doesn't call main() so I guess it's acceptable to use (); but there's no reason to.. and anyway, it is legal to call main() so you may as well declare it as taking no arguments if that's what you mean:
    Code:
    int main() { main(1); } /* this requires no diagnostic */
    int main(void) { main(1); } /* this does */
    It is actually "undefined behaviour" to call main from your own code [unless "your own code" is the startup that gets things going before main itself - as you may know, under normal circumstance, main() is the first piece of "your" C code, but it's not actually where the OS gets to when the application starts - there's a bit of preparation that the C runtime does first].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    I am not a C programmer, but from what I understand that is incorrect. To specify a variable number of arguments, one should either declare the function with an empty parameter list, or end the parameter list with an ellipsis.
    Yes, that is correct, there are only two ways (avoiding warnings/errors) to make a function take variable number of arguments:
    Code:
    int varfunc1();   /* Old style C function with variable number of args */
    int varfunc2(int x, ...);   /* ANSI-C function with variable number of args */
    A function specified with 2 arguments can not be called with 3 arguments in C or C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,709
    It is actually "undefined behaviour" to call main from your own code [unless "your own code" is the startup that gets things going before main itself - as you may know, under normal circumstance, main() is the first piece of "your" C code, but it's not actually where the OS gets to when the application starts - there's a bit of preparation that the C runtime does first].
    Those are the rules for C++, but as far as I can tell it does not apply to C.

    Yes, that is correct, there are only two ways (avoiding warnings/errors) to make a function take variable number of arguments:
    Thanks for the confirmation
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21