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.
I just released Vidrio, a free app for macOS and Windows to make your screen-sharing awesomely holographic. Vidrio shows your webcam video on your screen, just like a mirror. Then you just share or record your screen with Zoom, QuickTime, or any other app. Vidrio makes your presentations effortlessly engaging, showing your gestures, gazes, and expressions. #1 on Product Hunt. Available for macOS and Windows.

With Vidrio

With generic competitor

More by Jim

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