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?
When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters.
If in a parameter type list the only one parameter type is void (it must have no name then), then that means the function takes no arguments.
The void functions are called void because they do not return anything. “A void function cannot return anything” this statement is not always true. From a void function, we cannot return any values, but we can return something other than values.
void* is a "pointer to anything". void ** is another level of indirection - "pointer to pointer to anything". Basically, you pass that in when you want to allow the function to return a pointer of any type. &variable takes the address of variable.
The two canonical forms of main
are, according to the standard (see C99 section 5.1.2.2.2 "Program startup"):
int main (void);
int main (int argc, char *argv[]); // or equivalent such as char **argv
Others are specifically allowed but those are the required ones.
As to the preferred form between fn(void)
and fn()
, 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:
The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.
Paragraph 14 of that same section shows the only difference:
An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.
In other words, it means the same as void
in 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 but int fn(void);
means there are no parameters.
That means that:
int fn();
int fn (int x) { return x; }
int main (void) { return fn(0); }
is valid but:
int fn(void);
int fn (int x) { return x; }
int main (void) { return fn(0); }
is not.
In addition to paxdiablo's reply, which covers the C language and thus the OP's question, it may be worth noting that "void style" is considered bad practice in C++, at least by Bjarne Stroustrup.
The reasons why are purely style-related, but the use of void as function parameter was actually banned from C++ in the early days before ISO standardization. The void syntax was eventually allowed though, to provide compatibility with C.
So to sum this up:
f(void)
because it enforces stricter typing (rational argument)f()
, or you will upset various C++ gurus (irrational argument)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With