This C/C++ function call puzzle is a puzzle that is intended to explore more about the behaviour of method calling in both the programming languages C and C++/.
The output of a method in C and C++ is different. Lets see what is the difference in calling methods in C and C++.
Let’s take an example and check the output of the below code in c and c++.
Example
void method() { // Print statement } int main() { method(); method(2); }
Output
For C++ −
Error : too many arguments to function ‘void method()’
For C −
Program runs without any error.
Logic behind the output and error
The compilers for C++ programming language treats the function method() as a function without any parameter whereas the same function in the C compiler works quite efficiently as the compiler treats the function method() as a function that can accept variable argument.
Due to this i C++ when we pass arguments to a method that has no parameters in its definition there will be an error prompt in the output window say ‘error: too many arguments to function ‘void func()’.
And in C, the passed arguments are accepted and the code inside the function is executed.