【发布时间】:2021-08-23 04:32:21
【问题描述】:
//Converts Farenheit tempretaure to into the celsius scale
#include <stdio.h>
#define FREEZING_PT 32.0f
#define FACTOR 5.0f/9.0f
int main(void)
{
float faren,c;
printf("Enter the Farenheit temperature: ");
scanf("%f",&faren);
float c = (faren - FREEZING_PT)*FACTOR;
printf("The required celsius tempreature is: %.1f\n", c);
return 0;
}
我是一个完整的 C 初学者,这可能非常初级,但我无法解决这里的问题。
在上面的代码中,我得到的返回值始终是整数值的摄氏温度,即使它是浮点类型。例如,如果华氏温度是 0°,那么摄氏度的结果应该是 -17.7°,但我得到的结果只有 -17°。
修改后的代码:
//Converts Farenheit tempretaure to into the celsius scale
#include <stdio.h>
#define FREEZING_PT 32.0f
#define FACTOR 5.0f/9.0f
int main(void)
{
float faren,c;
printf("Enter the Farenheit temperature: ");
scanf("%f",&faren);
c = (faren - FREEZING_PT)*FACTOR;
printf("The required celsius tempreature is: %.1f\n", c);
return 0;
}
【问题讨论】:
-
您已经声明了两次
c。第一个在这里:float faren,c;,第二个在这里:float c = (faren - FREEZING_PT)*FACTOR;。从float c = (faren - FREEZING_PT)*FACTOR;中删除float -
您的代码甚至不应该编译,因为您两次声明
c。这是您实际程序的问题,还是在您将代码复制到问题时发生这种情况? -
我去掉了,对输出没有影响?
-
"摄氏度的结果应该是-17.7°," -->更像是-17.8。
-
@Elliott 好吧,请记住这一点
标签: c temperature