【问题标题】:C language not giving me the output in decimalsC语言不给我小数输出
【发布时间】: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


【解决方案1】:

在您的代码中,您已经两次声明了变量“c”。删除变量“c”的第一个声明后,它可以正常工作。

我得到了正确的输出。

输入华氏温度:0

所需的摄氏温度是:-17.8

输入华氏温度:1

所需的摄氏温度是:-17.2

输入华氏温度:2

所需的摄氏温度是:-16.7

删除变量“c”的第一个声明。它应该可以工作。

【讨论】:

  • 是的,这是我的一个小错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
  • 2021-06-21
  • 1970-01-01
  • 2017-04-08
相关资源
最近更新 更多