double类型数据的输入只能用%lf
输出可以用%f与%lf;
例题:
输入r,h求圆柱体的表面积,保留三位小数
original:
#include<stdio.h>
int main(){
double r,h; //%f只能输入float类型的数据
scanf("%f %f",&r,&h);
float area = 2*3.1415926*r*h;//pi不精确
printf("%.3f",area);
return 0;
}
after:
#include<stdio.h>
#include<math.h>
int main(){
const double pi = 4*atan(1.0);//excellent!
double r,h,s1,s2;
scanf("%lf%lf",&r,&h);
s1 = 2*pi*r*r;
s2 = 2*pi*r*h;
printf("%.3f",s1+s2);
return 0;
}