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

Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!

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