码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-07-14 15:31:09      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

 


原文链接: 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);
}

 

 

 

 

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!