在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 ",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 ",y);
}

 

 

 

 

原文地址:https://www.cnblogs.com/huhu0013/p/4645378.html