280

Consider these two function definitions:

void foo() { }

void foo(void) { }

Is there any difference between these two? If not, why is the void argument there? Aesthetic reasons?

1

4 Answers 4

356

In C:

  • void foo() means "a function foo taking an unspecified number of arguments of unspecified type"
  • void foo(void) means "a function foo taking no arguments"

In C++:

  • void foo() means "a function foo taking no arguments"
  • void foo(void) means "a function foo taking no arguments"

By writing foo(void), therefore, we achieve the same interpretation across both languages and make our headers multilingual (though we usually need to do some more things to the headers to make them truly cross-language; namely, wrap them in an extern "C" if we're compiling C++).

18
  • 13
    But if C++ had required the void, then it could have avoided the "most vexing parse" problem. Jan 4, 2010 at 17:47
  • 7
    True, but there are so many other crappy parses in C++ there's no real point in kvetching about any one of them.
    – DrPizza
    Jan 4, 2010 at 18:49
  • 16
    On a recent question, @James Kanze posted an interesting tidbit. Repost here to avoid losing it: the first versions of C did not allow to specify the number of parameters a function might take, thus void foo() was the only syntax to declare a function. When signatures where introduced, the C committee had to disambiguate the no-parameter from the old syntax, and introduced the void foo(void) syntax. C++ took it for the sake of compatibility. Sep 14, 2011 at 8:14
  • 3
    Can you give me an example of C C90 and later where using void foo() instead of void foo(void) will produce a functional difference? I.e. I have been using the version without the void for many years and havent seen any problem, am I missing something?
    – chacham15
    Nov 7, 2011 at 8:30
  • 12
    @chacham15 void foo() { if ( rand() ) foo(5); } compiles and runs (causing undefined behaviour unless you're very lucky), whereas void foo(void) with the same body would cause a compilation error.
    – M.M
    Dec 22, 2015 at 22:35
42

I realize your question pertains to C++, but when it comes to C the answer can be found in K&R, pages 72-73:

Furthermore, if a function declaration does not include arguments, as in

double atof();

that too is taken to mean that nothing is to be assumed about the arguments of atof; all parameter checking is turned off. This special meaning of the empty argument list is intended to permit older C programs to compile with new compilers. But it's a bad idea to use it with new programs. If the function takes arguments, declare them; if it takes no arguments, use void.

1
  • But the question is about definitions, in that case the relevant C rule is An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters.
    – jinawee
    Dec 26, 2018 at 12:56
10

C++11 N3337 standard draft

There is no difference.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf

Annex C "Compatibility" C.1.7 Clause 8: declarators says:

8.3.5 Change: In C ++ , a function declared with an empty parameter list takes no arguments. In C, an empty parameter list means that the number and type of the function arguments are unknown.

Example:

int f();
// means int f(void) in C ++
// int f( unknown ) in C

Rationale: This is to avoid erroneous function calls (i.e., function calls with the wrong number or type of arguments).

Effect on original feature: Change to semantics of well-defined feature. This feature was marked as “obsolescent” in C.

8.5.3 functions says:

4. The parameter-declaration-clause determines the arguments that can be specified, and their processing, when the function is called. [...] If the parameter-declaration-clause is empty, the function takes no arguments. The parameter list (void) is equivalent to the empty parameter list.

C99

As mentioned by C++11, int f() specifies nothing about the arguments, and is obsolescent.

It can either lead to working code or UB.

I have interpreted the C99 standard in detail at: https://stackoverflow.com/a/36292431/895245

1
  • The syntax highlighting is off near "means int". Can you fix it? For instance, turned off inside comments or "void" more like here. Nov 12, 2021 at 4:37
3

In C, you use a void in an empty function reference so that the compiler has a prototype, and that prototype has "no arguments". In C++, you don't have to tell the compiler that you have a prototype because you can't leave out the prototype.

1
  • 2
    "prototype" means the argument list declaration and return type. I say this because "prototype" confused me as to what you meant at first.
    – Zan Lynx
    Oct 1, 2008 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.