C中带小数点的计算问题

C中带小数点的计算问题,c,floating-point,double,decimal-point,C,Floating Point,Double,Decimal Point,我试图对灰度进行编码,但在计算时遇到了问题。有人能解释为什么返回27.00000而不是27.66667吗 #include <math.h> #include <stdio.h> int main(void) { float count = ((27 + 28 + 28) / 3); printf("%f\n", count); } 你忘了选演员了: int main() { float count = ((27 + 2

我试图对灰度进行编码,但在计算时遇到了问题。有人能解释为什么返回27.00000而不是27.66667吗

#include <math.h>
#include <stdio.h>

int main(void)
{

    float count = ((27 + 28 + 28) / 3);
    printf("%f\n", count);

}
你忘了选演员了:

int main()
{
    float count = ((27 + 28 + 28) / (float)3);
    printf("%f\n", count);

    return 0;
}
或:


您正在执行int/int操作,因此是27。将其更改为27+28+28/3.0或显式强制转换27+28+28/float 3有很多重复项:,谢谢!成功了。为了安全起见:每当我使用硬编码的数字时,它都会自动进行整数除法,除非我在数字前加上小数点或浮点??这取决于操作数的类型。在两个整数之间,结果将是一个整数。在一个整数和一个浮点之间,结果将是一个浮点。@chux,我指的是第一个示例,其中数字被转换成浮点。关于第二个,你是对的。@MatteoGalletta,你是对的。
int main()
{
    float count = ((27 + 28 + 28) / 3.0);
    printf("%f\n", count);

    return 0;
}