屏幕中显示的输出与原始结果不匹配

The output shown in the screen is not matching with the original result

我是 C 的初学者

我在使用以下代码时遇到了一些问题:-

Code

#include <stdio.h>

int main()
{
    int a = 7, b;
    b = a * 0.621;
    printf("%f", (float)b);
    return 0;
}

输出应该是 4.347,但在这里,输出是 4.000000

我应该怎么做才能得到 4.347 而不是 4.000000

'b'是int类型,所以它只会存储4,因为你在打印中使用的是(float),所以它会将值4显示为4.000000。将 b 的数据类型更改为 float 或 double。

int a=7,b;

应该改为

int a=7;float b;

需要将int类型改为double类型,可以包含十进制数。

int main()
{
    double a=7 , b;
    b = a*0.621;
    printf("%f",(b));
    return 0;
}

输出:4.34700