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?
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?
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.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 */
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.Originally Posted by cas
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.Originally Posted by Elysia
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Yes, that is correct, there are only two ways (avoiding warnings/errors) to make a function take variable number of arguments:
A function specified with 2 arguments can not be called with 3 arguments in C or C++.Code:int varfunc1(); /* Old style C function with variable number of args */ int varfunc2(int x, ...); /* ANSI-C function with variable number of args */
--
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.
Those are the rules for C++, but as far as I can tell it does not apply to C.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].
Thanks for the confirmationYes, that is correct, there are only two ways (avoiding warnings/errors) to make a function take variable number of arguments:
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)