c语言float和double的问题。

# include <stdio.h>

void main()
{
double r,s,v;
printf("Please enter the value of r:\n");
scanf("%f",&r);
s=4.0*3.14159*r*r;
v=4.0/3.0*3.14159*r*r*r;
printf("s=%f\nv=%f\n",s,v);
}

此程序运行结果如图所示,如果变量r,s,v都用float定义的话,运行结果就是正常的。
请问这是为什么?

scanf 时,格式定义和类型必须一致,输出时可以当成float型,但可能会有精度和溢出的问题。
所以你可以只把下面的改成lf
scanf("%f",&r); --------> scanf("%lf",&r);
这儿是原因:
Because C will promote floats to doubles for functions that take variable arguments. Pointers aren't promoted to anything, so you should be using %lf or %g to read in doubles.
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-04-07
double 输入输出格式要用 %lf
字母 l -Long 的意思,
相似回答
大家正在搜