c - 我面临段错误(代码已转储)

标签 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]|

后来,当我运行该程序时,在输入“矩形的宽度”后,它显示分割错误

最佳答案

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

scanf("%lf", &height);

关于c - 我面临段错误(代码已转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58454653/

相关文章:

使用c或任何编程语言控制usb端口上的电压供应

c - 如何正确设置 ALSA 设备

c - 如何将 const void* 转换为结构元素?

在多平台上比较 2 struct tm

c - 所有连续子数组的最大和

c - 带有可移动光标的 NCurses getstr()?

c - 我怎样才能终止系统()?

c - C 中的 Scanf 文本溢出

c - C99 标准是否保证 unsigned int 的二进制表示?

c - C语言中如何改变指针的值