This question already has answers here:
Division result is always zero [duplicate]
                                
                                    (4个答案)
                                
                        
                        
                            Dividing 1/n always returns 0.0 [duplicate]
                                
                                    (3个答案)
                                
                        
                                2年前关闭。
            
                    
我陷入了代码中,必须将用户给定的温度(以华氏度为单位)转换为摄氏度。但不幸的是,它的公式不适用于C编译器。

#include<stdio.h>
#include<stdlib.h>
void c_f();
//void f_c();
float c,f,fc,fc1;

int main()
{
    c_f();
    //  f_c();

return 0;
getch();
}
void c_f()
{
    printf("\n Enter the temperature (in *F) to covert it into Celsius: ");
    scanf("%d",&f);

    fc=((5/9)*(f-32));
    printf("\n %f*C",fc);

}

最佳答案

fc=((5/9)*(f-32));


因为5和9是整数,所以算术运算是在整数级别完成的,因此是5/9 == 0

您应该将其更改为浮点数:

fc = ((5.0f / 9.0f) * (f - 32.0f));


此外,格式说明符%d用于整数。如果要读取浮点数,请使用%f

scanf("%f", &f);


结果应该是现在。

关于c - C编译器不采用5/9来计算fc ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47482650/

10-11 15:23