在C中,函数的输出始终为0.000000.是因为两个输入都是int?

0 c int function output

我知道你通常不打算把你的所有代码都用掉,但这很短,并且有助于解决这个问题.任何人都可以解释为什么输出为0以及如何更改代码以输出应该是锥体的体积.

#include <stdio.h>

float ConeVolume(int height, int radius);

float ConeVolume(int height, int radius)
{
    float pi;
    pi = 3.14159;
    float third;
    third = (1/3);
    float vol;
    vol = third * pi * radius * radius * height;
    return vol;
}


int main()
{
float x = ConeVolume(12,10);
printf("%.4f \n", x);
}
Run Code Online (Sandbox Code Playgroud)

编辑:谢谢所有回答如此之快的人.这里很棒的社区.

alk*_*alk 7

1/3
Run Code Online (Sandbox Code Playgroud)

是一个整数除法,总是导致0.

要将此值计算为可能的浮点变量

1./3
Run Code Online (Sandbox Code Playgroud)

要么

1/3.
Run Code Online (Sandbox Code Playgroud)

要么

1./3.
Run Code Online (Sandbox Code Playgroud)

甚至更明确

(float)1/(float)3
Run Code Online (Sandbox Code Playgroud)

例如.