Possible Duplicate:
C void arguments
I just started with C and I can’t find answer to this…
Is there any difference between
int foo() { }
int foo(void) { }
Which should I prefer and why?
Note that this question also goes for: int main. Should it be: int main or int main(void) when I don’t want any command-line arguments?
The two canonical forms of
mainare, according to the standard (see C99 section 5.1.2.2.2 “Program startup”):Others are specifically allowed but those are the required ones.
As to the preferred form between
fn(void)andfn(), I prefer the former since I like to explicitly state that there are no parameters.There is also a subtle difference. C99 section 6.7.5.3 “Function declarators”, paragraph 10, states:
Paragraph 14 of that same section shows the only difference:
In other words, it means the same as
voidin the function definition but does not mean that in a standalone declarator (i.e., the prototype).int fn();means that no information on the parameters is yet known butint fn(void);means there are no parameters.That means that:
is valid but:
is not.