c语言中隐式参数声明,声明声明符之外的参数

C标准规定,对于函数定义,如果声明符包含标识符列表,则参数的类型应在以下声明列表中声明.显然这会产生影响.

extern int max(int a,int b)

{

return a > b ? a : b;

}

extern int max(a,b)

int a,b;

{

return a > b ? a : b;

}

Here int a,b; is the declaration list for the parameters. The

difference between these two definitions is that the first form acts

as a prototype declaration that forces conversion of the arguments of

subsequent calls to the function,whereas the second form does not.

这对程序员意味着什么,它是否会影响编译器生成的代码?

301_16@

这意味着在第二种情况下,调用者有责任确保提供的参数类型正确;不会提供隐式转换(默认参数促销除外).从6.5.2.2节:

If the expression that denotes the called function has a type that does not include a

prototype,the integer promotions are performed on each argument.

If the expression that denotes the called function has a type that does include a prototype,the arguments are implicitly converted,as if by assignment,to the types of the

corresponding parameters.

所以调用这样的代码就可以了:

char x = 3;

char y = 7;

max(x,y); // Equivalent to max((int)x,(int)y)

因为x和y在放入堆栈之前被提升为int.

但是,像这样的代码是不可行的:

double x = 3.0;

long y = 7;

max(x,y); // Uh-oh

x和y将以double和long的形式放在堆栈中,但max()将尝试读取两个int,这将导致未定义的行为(实际上,原始位将被重新解释).

这是不使用第二种形式的一个原因;它在标准中的唯一原因是提供与(极端)遗留代码的向后兼容性.如果您正在使用GCC,则可以使用-Wold-style-definition标志强制执行此操作;我希望其他编译器能提供相同的东西.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值