#include<stdio.h>
int foo();
int bar();
int main(void)
{
foo(2,3,4);
return 0;
}
int foo(int a, int b)
{
return bar(a);
}
int bar(int c, int d)
{
int e = c + d;
printf("%d",e);
return e;
}
输出
5
为什么bar()
函数可以接收两个参数?
最佳答案
使用空括号作为参数列表的函数声明意味着,函数可以接收的参数的数量和类型没有限制(和检查)。可以使用任意数量和类型的参数来调用它。
FWIW,如果您想指定一个不带参数的函数,则需要指定 void
作为参数列表,例如
int foo(void);
引用 C11
,第 §6.5.2.2 章,函数调用
... If the number of arguments does not equal the number of parameters, the behavior is undefined...
因此,您的程序调用 undefined behaviour 。绝对无法保证或预测您的应用程序将如何运行。
关于c - 非可变参数函数如何接受不同数量的参数(与其定义的参数数量不同)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31238890/