在C函数调用 [英] function call in C
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/12643202/why-does-gcc-allow-arguments-to-be-passed-to-a-function-defined-to-be-with-no-ar\">Why不GCC允许参数传递给定义为不带参数的函数吗?
在code:
#include <stdio.h>
#include <stdlib.h>
void test_print()
{
printf("test print\n");
}
int main()
{
test_print(1,2);
return 0;
}
虽然test_print的主要调用者有此功能的界定及不同金额的参数,code可以工作得很好,但如果把它变成C ++版本,出现编译错误太多的参数功能可按......。
为什么C允许的函数参数不匹配调用,我们什么时候可以使用电话的这种方式?为什么它forbidened在C ++中。
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++.
系统的Ubuntu 11.10结果
编译器:GCC 4.6.1
System ubuntu 11.10
compiler: gcc 4.6.1
推荐答案
在C空()
上的函数表示该功能可以采取任何数量的参数。
如果要指定函数不接受你需要的任何参数:
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)
虽然在C ++中的空()
上的功能意味着它不带任何参数。请注意,这是在显著C ++,因为C ++的基础上,它可以采取一些参数就可以重载函数。
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.
这篇关于在C函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!