简体   繁体   English

C 语言没有给我小数形式的 output

[英]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;
}

Im a Complete C beginner and this may be very elementary but I cannot figure out the problem here.我是一个完整的 C 初学者,这可能是非常初级的,但我无法弄清楚这里的问题。

In the above code, the value I get returned is always the Celsius temperature in integer values, even if it is a float type.在上面的代码中,我返回的值始终是 integer 值中的摄氏温度,即使它是 float 类型。 For example, if the Fahrenheit temperature was 0°, the result in Celsius should be -17.7°, but I get the result only as -17°.例如,如果华氏温度为 0°,则摄氏温度的结果应为 -17.7°,但我得到的结果仅为 -17°。

Edited code:编辑代码:

//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;
}

In your code, you've declared the variable 'c' twice.在您的代码中,您已两次声明变量“c”。 After removing the first declaration of the variable 'c', it's working properly.删除变量“c”的第一个声明后,它可以正常工作。

I'm getting the correct output.我得到正确的 output。

Enter the Farenheit temperature: 0输入华氏温度:0

The required celsius tempreature is: -17.8所需摄氏温度为:-17.8

Enter the Farenheit temperature: 1输入华氏温度:1

The required celsius tempreature is: -17.2所需摄氏温度为:-17.2

Enter the Farenheit temperature: 2输入华氏温度:2

The required celsius tempreature is: -16.7所需摄氏温度为:-16.7

Remove the first declaration of the variable 'c'.删除变量“c”的第一个声明。 It should work.它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM