Possible Duplicate:
Why does gcc allow arguments to be passed to a function defined to be with no arguments?
the code:
#include <stdio.h>
#include <stdlib.h>
void test_print()
{
printf("test print\n");
}
int main()
{
test_print(1,2);
return 0;
}
although the caller of test_print in main has different amount of arguments with the defination of this function, the code can work very well, but if change it into c++ version, there occurs a compile error "too many arguments to funciton ....". why C allows argument mismatch call of function, when can we use this way of calling? and why it's forbidened in c++.
System ubuntu 11.10
compiler: gcc 4.6.1
In c empty ()
on an function means the function can take any number of arguments.
If you want to specify that function does not take any arguments you need to:
void test_print(void)
While, in C++ empty ()
on an function means it does not take any arguments. Note that this is significant in C++ because in C++ you can overload a function based on number of arguments it can take.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With