首页 > 解决方案 > C程序返回矩形的周长和面积

问题描述

我想编写 ac 程序来打印矩形的面积和周长,但出现分段错误错误。

#include <stdio.h>


int rectanglePerimeter(int h, int w)
{
    
    int p =(2*h)+(2*w);
    return p;
}

int rectangleArea(int h, int w)
{
    int a = h * w;
        return a;   
}

int main()
{
    int h, w, perimeter, area;
    printf("Tell me the height and width of the rectangle\n");

    scanf("%i", h);
    scanf("%i", w);
    
    perimeter = rectanglePerimeter(h, w);

    area = rectangleArea(h, w);

    printf("Perimeter of the rectangle = %i inches", perimeter);

    printf("Area of the rectangle = %i square inches", area);
    
    return 0;
}

有人可以向我解释我做错了什么吗?

标签: c

解决方案


在使用 C 编程语言时,某些函数需要使用内存地址,scanf函数就是这种情况。Scanf 函数接受参数,第一个是您期望的数据类型,int、float、char 等;第二个参数是你将保存这个输入的地方,这个地方不是你要保存的变量的引用,而是变量的内存地址。这种方式scanf应将读取的值保存在您存储的变量中。

scanf("%i", &h);
scanf("%i", &w);

推荐阅读