问题
编译平台是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