如果我乘以不带括号的数字来获得华氏温度,则得到的数字将不同于与之相乘的数字。这是为什么?

#include <stdio.h>

int main (){
    double userinput, fahr1, fahr2;

    printf("Enter a number in Cels ");
    scanf("%lf", &userinput);

    fahr1 = userinput*(9/5)+32;
    fahr2 = userinput*9/5+32;

    printf("fahr1 = %f and fahr2 = %f \n", fahr1, fahr2);

    return 0;
}

输出:

Enter a number in Cels 15
fahr1 = 47.000000 and fahr2 = 59.000000

最佳答案

(9/5)是整数除法,其结果为1。但是userinput*9被评估为双精度,然后除以5也被视为双精度。

09-25 19:16