浮点数据类型
1. 处理带小数的数值
我们来讨论一下类似于1.234567,0.00001,这类非整数的数据。我们先试试看,int能不能装这些数据呢?
#include <stdio.h>
int main()
{
int a = 1.234567;
int b = 0.00001;
int c = 365.12345;
printf("%d\n", a);
printf("%d\n", b);
printf("%d\n", c);
return 0;
}
好像不太行,小数部分都丢失了。那怎么办呢?我们需要引入新的类型了。
2. 浮点类型
我们把上面的代码中int,用float替代。
printf("%d\n",a);中的占位符%d,我们知道%d是用于整数的,因此%d肯定是不行的。所以我们用%f替换。
#include <stdio.h>
int main()
{
float a = 1.234567;
float b = 0.00001;
float c = 365.12345;
printf("%f\n", a);
printf("%f\