printf 在打印双精度数时打印零
问题描述:
我有一段 C 代码,用于计算周长.无论我在变量 Z 中输入什么,它总是打印 0.000000
I have a piece of code in C, which is supposed to compute the circumference.
No matter what I put in for variable Z when asked for it, it always prints 0.000000
有什么想法吗?
#include <stdio.h>
int main()
{
double pi = 3.1415926;
double z = 0.0;
printf("What is the radius of the circle? \n ");
scanf("%1f", &z);
double c = 2.0 * pi * z;
printf("The circumference is %1f", c);
return 0;
}
答
将 %1f
更改为 %lf
.
像这样:
#include <stdio.h>
int main()
{
double pi = 3.1415926;
double z = 0.0;
printf("What is the radius of the circle? \n ");
scanf("%lf", &z);
double c = 2.0 * pi * z;
printf("The circumference is %lf", c);
return 0;
}