float与double的存储

caicaihello 2009-10-17 10:39:42
#include"stdlib.h"

#define PI 3.14

void main()
{
double r=0; //raius
double c=0;//circumference of the circle
double a=0;//the area of the circle

//initialize the raius of the circle
printf("Please input the radius(radus>0):");
scanf("%f",&r);

//compute the circumference of the circle

c=2*PI*r;
printf("The circumference is %lf",c);

//compute the area of the circle

a=PI*r*r;
printf("\nThe area is %lf\n",a);


}



以上程序不能输出正确结果,原因是scanf("%f",&r)中输入格式控制符是%f,而不是%lf,我想问的是double和float的存储时什么样的,为什么这个程序会输出0.00000?
...全文
121 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pang123hui 2009-10-18
  • 打赏
  • 举报
回复
第一个问题LS解决了

第二个问题,首先LZ肯定打错了。应该是0.000000,也就是小数位有6位

这是printf函数的规定,最多6位,不信LZ可以用printf多输入几个数试试就知道了
yuanwenqun2 2009-10-18
  • 打赏
  • 举报
回复
UP
昵称很不好取 2009-10-17
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>

#define PI 3.14

int main()
{
float r=0; //这里用float就可以了,double类型要分配更多的空间,要用%lf告诉编辑器
double c=0;
double a=0;

printf("Please input the radius(radus>0):");
scanf("%f",&r);

c=2*PI*r;
printf("The circumference is %lf",c);

a=PI*r*r;
printf("\nThe area is %lf\n",a);

return 0;
}

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧