c 中意外的 printf 语句
Unexpexted printf statement in c
我正在制作一个将摄氏温度转换为华氏温度和开尔文度的小程序
它使用一个函数,该函数将指向 int 的指针作为参数并且 returns Fahrenait.When 程序结束我必须更改 akc 整数的值,这是我将摄氏温度保存为开尔文度数的地方
这是我所做的。
float thermo(int *);
int main(){
int akc;
akc=100;
printf("%dce = %f = %dK\n",akc,thermo(&akc),akc);
system("pause");
return 0;
}
float thermo(int *akc){
float a=*akc;
*akc+=273;
return 9*a/5+32;
}
我的问题是,当我打印所有值时,我得到以下输出:
373 摄氏度 = 212.000000 华氏度 = 100 开尔文
但结果应该是
100 摄氏度 = 212.000000 华氏度 = 373 开尔文
有什么想法吗?
printf("%dce = %f = %dK\n",akc,thermo(&akc),akc);
函数参数的求值顺序在 C 中未指定。您不能假设第一个参数将被求值,然后是第二个,依此类推。要解决此问题,您可以将结果保存在临时变量中。
我正在制作一个将摄氏温度转换为华氏温度和开尔文度的小程序 它使用一个函数,该函数将指向 int 的指针作为参数并且 returns Fahrenait.When 程序结束我必须更改 akc 整数的值,这是我将摄氏温度保存为开尔文度数的地方 这是我所做的。
float thermo(int *);
int main(){
int akc;
akc=100;
printf("%dce = %f = %dK\n",akc,thermo(&akc),akc);
system("pause");
return 0;
}
float thermo(int *akc){
float a=*akc;
*akc+=273;
return 9*a/5+32;
}
我的问题是,当我打印所有值时,我得到以下输出:
373 摄氏度 = 212.000000 华氏度 = 100 开尔文
但结果应该是
100 摄氏度 = 212.000000 华氏度 = 373 开尔文
有什么想法吗?
printf("%dce = %f = %dK\n",akc,thermo(&akc),akc);
函数参数的求值顺序在 C 中未指定。您不能假设第一个参数将被求值,然后是第二个,依此类推。要解决此问题,您可以将结果保存在临时变量中。