c编程:答案始终等于0 [重复]

46scxncf  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(68)

此问题已在此处有答案

Why does scanf require &?(7个回答)
24天前关闭

#include <stdio.h>

int main(){

    double Radius, Circumference, Area, Pi = 3.1416;
    
    printf("Value of Radius:");
    scanf("%lf",Radius);
    
    Area = Pi * Radius * Radius;
    Circumference = 2 * Pi * Radius;
    
    printf("The area of the circle is %lf", Area);
    printf("\nThe circumference of the circle is %lf", Circumference);
    
    return 0; 
}

我希望能得到周长和面积,只需要把半径代入。

ttygqcqt

ttygqcqt1#

scanf函数需要指针。

scanf("%lf", &Radius);

你应该已经收到一个警告(启用警告!)编译此:

test.c: In function ‘main’:
test.c:8:14: warning: format ‘%lf’ expects argument of type ‘double *’, but argument 2 has type ‘double’ [-Wformat=]
     scanf("%lf",Radius);
            ~~^

您还应该检查scanf的返回值,以确保它实际上读取了正确数量的值。

相关问题