[英]C language not giving me the output in decimals
//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 初学者,这可能是非常初级的,但我无法弄清楚这里的问题。
在上面的代码中,我返回的值始终是 integer 值中的摄氏温度,即使它是 float 类型。 例如,如果华氏温度为 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”。 删除变量“c”的第一个声明后,它可以正常工作。
我得到正确的 output。
输入华氏温度:0
所需摄氏温度为:-17.8
输入华氏温度:1
所需摄氏温度为:-17.2
输入华氏温度:2
所需摄氏温度为:-16.7
删除变量“c”的第一个声明。 它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.