printf打印双精度时打印零

时间:2018-08-29 20:45:19

标签: c printf

我在C语言中有一段代码,应该用来计算周长。 无论我在输入变量Z时输入什么,它始终会显示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;
}

3 个答案:

答案 0 :(得分:2)

%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;
}

答案 1 :(得分:0)

要读成z的两倍,您必须使用scanf("%lf", &z)而不是"%1f"

答案 2 :(得分:0)

您非常亲密。试试这个

#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;
} 

您的逻辑告诉浮点数,它需要一个整数,但此后不需要小数点