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); }
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.