TheHawk3r
TheHawk3r

Reputation: 79

C program to return perimeter and area of a rectangle

I want to write a c program to print the area and perimeter of a rectangle but i am getting a segmentation fault error.

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

Can somebody explain to me what am i doing wrong ?

Upvotes: 1

Views: 1584

Answers (1)

Joao Pedro P.P
Joao Pedro P.P

Reputation: 137

When working with C programing language some functions require to work with memory address, that is the case with scanf function. Scanf function takes two arguments, the first is the type of data you are expecting, a int, a float, a char, etc; and second argument is where you will save this input, and this place is not a referece of the variable you are going to save but the memory address of the variable. This way scanf shall save the value read in the variable you are storing it.

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

Upvotes: 2

Related Questions