What does void
mean as a function parameter in C?
What is the difference between
int foo(void);
and
int foo();
I thought: nothing. But there is: the former takes no arguments, whereas the latter takes an unspecified number of arguments!
The difference is illustrated by calling the function with different numbers of arguments. This compiles (but emits a warning):
int foo() { return 4; }
int main(void) {
foo("bar");
return 0;
}
whereas this does not compile:
int foo(void) { return 4; }
int main(void) {
foo("bar");
return 0;
}
% clang void_fn.c
void_fn.c:4:7: error: too many arguments to function call, expected 0, have 1
foo("bar");
~~~ ^~~~~
void_fn.c:1:1: note: 'foo' declared here
int foo(void) { return 4; }
^
1 error generated.
Tagged . All content copyright James Fisher 2016. This post is not associated with my employer.