在C语言中,我的函数输出始终为0.000000.是因为两个输入都是int吗? [英] In C, my output of a function is always 0.000000. Is it because the two inputs are int?
本文介绍了在C语言中,我的函数输出始终为0.000000.是因为两个输入都是int吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道您通常不打算发布所有代码,但这很简短,可以帮助解决问题.谁能解释输出为什么为0以及如何更改代码以输出应为圆锥体体积的东西.
I know you're usually not meant to put out all of your code but this is short and would help with the problem. Can anyone please explain why the output is 0 and how I can change the code to output what should be the volume of a cone.
#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);
}
感谢所有回答如此迅速的人.这里的社区很棒.
edit: thank you to all who answered so quickly. Great community here.
推荐答案
1/3
是整数除法,总是结果为 0
.
is an integer division and always results in 0
.
要对此求值为浮点变量,可以执行
To have this evaluate to a floating point variable you might do
1./3
或
1/3.
或
1./3.
或更明确的
(float)1/(float)3
例如.
这篇关于在C语言中,我的函数输出始终为0.000000.是因为两个输入都是int吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文