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 #c, #programming, #semantics.

Similar posts

More by Jim

👋 I'm Jim, a full-stack product engineer. Want to build an amazing product and a profitable business? Read more about me or Get in touch!

This page copyright James Fisher 2016. Content is not associated with my employer. Found an error? Edit this page.