向c函数传递超出要求的参数 [英] passing more than required arguments to c function
问题描述
如果我向函数传递的参数超出要求,会发生什么?我希望所调用的函数中有损坏的东西,但是在一些小的测试代码中,一切都可以正常工作。
What happens if I pass more arguments than required to a function? I expected something to be corrupted in the called function, but everything works fine in some small test codes.
例如:
void print()
{
int x=10;
printf("%d\n",x);
}
void main()
{
print(0,0,0,0,0);
}
推荐答案
这是未定义的行为。
(C99,6.5.2.2p6)如果表示被调用函数的表达式的类型不包含
原型,[...]如果参数的数量不等于参数的数量
,则行为未定义。
从6.9.1p7中我们知道 print
函数没有提供原型。
And we know from 6.9.1p7 that print
function does not provide a prototype.
C99,6.9.1p7)如果声明符包括参数类型列表,则
列表还将指定所有参数的类型;这样的声明符
还可以作为函数原型,以便以后在同一转换单元中调用同一函数
。如果声明符包括标识符
列表,142)参数类型应在$ b $中声明b以下声明清单。
C99, 6.9.1p7) "If the declarator includes a parameter type list, the list also specifies the types of all the parameters; such a declarator also serves as a function prototype for later calls to the same function in the same translation unit. If the declarator includes an identifier list,142) the types of the parameters shall be declared in a following declaration list."
由于不存在约束冲突,因此不需要诊断消息。
As there is no constraint violation, no diagnostic message is required.
这篇关于向c函数传递超出要求的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!