我遇到分段错误(代码已转储)

时间:2019-10-18 16:14:39

标签: c

我是在练习“ C”语言时初学者编写此简单代码的。此处的目的是测量输入值的周长和面积。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    double width;
    double height;
    double perimeter;
    double area;

    printf("Enter the Width of Rectangle:");
    scanf("%f", width);

    printf("Enter the height of the Rectangle:");
    scanf("%f", height);

    perimeter = 2*(width+height);

    area = width*height;

    printf("Perimeter of the Rectangle=%f\n", perimeter);
    printf("Area of the Rectangle=%f\n", area);
    printf("Hence the problem is solved");

    return 0;
}

当我编译代码时。每次都会给我这个特定的错误。

D:\Mandeep\prog pro\Learning C\Learning_1.c||In function 'main':|

D:\Mandeep\prog pro\Learning C\Learning_1.c|12|warning: format '%f' expects argument of type 'float *', but argument 2 has type 'double *' [-Wformat=]|

D:\Mandeep\prog pro\Learning C\Learning_1.c|15|warning: format '%f' expects argument of type 'float *', but argument 2 has type 'double' [-Wformat=]|

D:\Mandeep\prog pro\Learning C\Learning_1.c|15|warning: 'height' is used uninitialized in this function [-Wuninitialized]|

稍后,当我运行该程序时,输入“矩形的宽度”后会显示分段错误

1 个答案:

答案 0 :(得分:1)

您对scanf的使用不正确。 scanf希望得到一个指针:

scanf("%lf", &height);