浮点数的函数传参不正确的问题

问题

编译平台是keil,编译和链接都没有报错,但是使用时传递给float_test的参数有错误。
test.c

void float_test(float r_shunt, float i_max)
{
	printf("11 shunt %f, i_max %f\n", r_shunt, i_max);
	i_max = 20.48;
	r_shunt = 0.004;
	printf("22 shunt %f, i_max %f\n", r_shunt, i_max);
}

main.c

int main(void)
{
	float_test(4.0, 20.0);
    return 0;
}

会出现错误的打印:

11 shunt -518969491456.000000, i_max 0.939000
22 shunt 0.004000, i_max 20.480000

解决办法

出现这个问题的原因是因为在main.c文件中出现隐式的调用float_test函数,编译时没有报错,但使时出现问题,具体为什么,暂时不知道,网上查的资料是因为“调用一个不带原型声明的函数时:调用者会对每个参数执行“默认实际参数提升(default argument promotions)”,也即将float变为double型。
解决办法是在main.c文件中包含test.h的头文件或者使用extern关键字。
main.c

extern void float_test(float r_shunt, float i_max);
int main(void)
{
	float_test(4.0, 20.0);
    re
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值