Skip to content

Why use foo(void) instead of just foo() – Why void in parameter list

In older C (pre ANSI), the way to declare and define a function is:

int test();


int test(param1, param2)
int param1, param2;
    // body of function

Current C/C++ still allow this kind of definition (try it out!), which means that if you specified int test(), it actually means test() with unspecified number of arguments (pending parsing when the compiler sees the actual definition of the function).

On a side note, if you did try using the old declaration and definition, notice that the compiler no longer performs type checking (so you can call the function with whatever arguments), and if you used the wrong arguments the program fails at runtime.

Posted in C/C++. Tagged with .