Reputation: 96016
Suppose I have this function:
void func() {}
When I call func
with some parameters (e.g. func(132)
), the C++
compiler yields an error, while the C
compiler doesn't.
What is the difference between the two compilers in this case? And what advantages/disadvantages the C++
has by arising this error?
Upvotes: 16
Views: 710
Reputation: 213837
There are no advantages or disadvantages. C supports this for compatibility with K&R C from the 1980s. You might like this feature if you are still using code you wrote in the 1980s. You might dislike this feature if you want better diagnostics from your compiler.
void func();
In C, this means that func
takes unspecified parameters.
If you need to specify that the function takes no parameters, write it this way:
void func(void);
In C++ the two prototypes are the same. (In C, only the second one is a prototype.) If you compile with GCC/Clang's -Wstrict-prototypes
option, you will get warnings for using void func();
in C, as you should.
This is only about function declarations. In both languages, the following function definitions are the same:
// These two are the SAME
void func() { }
void func(void) { }
// These two are DIFFERENT
void func();
void func(void);
Upvotes: 36
Reputation: 882726
In C++, that function has no arguments. In C, it means an indeterminate number of arguments.
This is a holdover from the very earliest days of C where every function returned an int
and it was much more relaxed about arguments being passed.
Upvotes: 8