在C语言中除法运算为什么没有小数部分?

 


原文链接: http://wenda.tianya.cn/question/4e096f010317a93d

除法运算符" / ",如果是两个整数相除结果为整数
如果需要保留小数时 必须将其中一个除数转换为浮点数

#include <stdio.h>
#include <math.h>
main()
{
float x;
float y;
printf("Enter x:");
scanf("%d",&x);
y=fabs((5*x+1)/(x*x+1));
printf("y is %f\n",y);
}

或者

#include <stdio.h>
#include <math.h>
main()
{
int x;
float y;
printf("Enter x:");
scanf("%d",&x);
y=fabs((float)(5*x+1)/(x*x+1));
printf("y is %f\n",y);
}

 

 

 

 

posted @ 2015-07-14 14:37  huhu0013  阅读(1086)  评论(0编辑  收藏  举报