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?
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.
In C, this means that
functakes unspecified parameters.If you need to specify that the function takes no parameters, write it this way:
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-prototypesoption, you will get warnings for usingvoid func();in C, as you should.This is only about function declarations. In both languages, the following function definitions are the same: