gotchacppCritical
Is there a difference between foo(void) and foo() in C++ or C?
Viewed 0 times
voidandbetweendifferencefoothere
Problem
Consider these two function definitions:
Is there a difference between these two? If not, why is the
void foo() { }
void foo(void) { }Is there a difference between these two? If not, why is the
void argument there? Is it for aesthetic reasons?Solution
Historical note: this answer applies to C17 and older editions. C23 and later editions treat
In C:
In C++:
By writing
void foo() differently.In C:
void foo()means "a functionfootaking an unspecified number of arguments of unspecified type"
void foo(void)means "a functionfootaking no arguments"
In C++:
void foo()means "a functionfootaking no arguments"
void foo(void)means "a functionfootaking 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++).Context
Stack Overflow Q#51032, score: 365
Revisions (0)
No revisions yet.