Thread: int main ()?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05: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, 06: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-02-2003, 12:26 AM