gpt4 book ai didi

c - 将超过要求的参数传递给 c 函数

转载 作者:太空宇宙 更新时间:2023-11-04 02:08:54 25 4
gpt4 key购买 nike

如果我向函数传递的参数多于所需参数,会发生什么情况?我预计被调用函数中的某些内容会被破坏,但在一些小的测试代码中一切正常。

例如:

void print()
{
int x=10;
printf("%d\n",x);
}
void main()
{
print(0,0,0,0,0);
}

最佳答案

这是未定义的行为。

(C99, 6.5.2.2p6) "If the expression that denotes the called function has a type that does not include a prototype, [...] If the number of arguments does not equal the number of parameters, the behavior is undefined."

并且我们从 6.9.1p7 中得知 print 函数不提供原型(prototype)。

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."

由于没有违反约束,因此不需要诊断消息。

关于c - 将超过要求的参数传递给 c 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17104787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com