Learn more about Russian war crimes in Ukraine.

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.

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged . All content copyright James Fisher 2016. This post is not associated with my employer. Found an error? Edit this page.