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?
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?
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.
In C:
void foo()
means "a functionfoo
taking an unspecified number of arguments of unspecified type"void foo(void)
means "a functionfoo
taking no arguments"In C++:
void foo()
means "a functionfoo
taking no arguments"void foo(void)
means "a functionfoo
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 anextern "C"
if we're compiling C++).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.5.3 functions says:
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
I realize your question pertains to C++, but when it comes to C the answer can be found in K&R, pages 72-73: