Chapter - 8: The C Preprocessor

Write macro definitions with arguments for calculation of area and perimeter of a triangle, a square, and a circle. Store these macro definitions in a file called "areaperi.h". Include this file in your program, and call the macro definitions for calculating area and perimeter for different squares, triangles, and circles.

#include "areaperi.h"

int main()
	int r, a, x, y, z;
	double cir_ar, cir_peri, sqr_ar, sqr_peri, tri_ar, tri_peri;

	printf("Enter the radius of the circle : ");
	scanf("%d", &r);
	printf("\nEnter the side of the square : ");
	scanf("%d", &a);
	printf("\nEnter the sides of the triangle : ");
	scanf("%d%d%d", &x, &y, &z);

	if (x + y>z && y + z>x && x + z>y)
		tri_ar = TRI_AREA(x, y, z);
		tri_peri = TRI_PERI(x, y, z);
		printf("\nArea : %lf\nPerimeter : %f\n", tri_ar, tri_peri);
		printf("\nThe triangle You entered is invalid.\n");

	cir_ar = CIR_AREA(r);
	cir_peri = CIR_PERI(r);
	sqr_ar = SQR_AREA(a);
	sqr_peri = SQR_PERI(a);
	printf("\nArea : %f\nPerimeter : %f\n", cir_ar, cir_peri);
	printf("\nArea : %f\nPerimeter : %f\n", sqr_ar, sqr_peri);
	return 0;

Make a file "areaperi.h" and put in the folder where this c source file is.

Code of areaperi.h is


#define PI 3.14

#define S(a,b,c) ((a+b+c)/2.0)

#define TRI_AREA(a,b,c) (sqrt((S(a,b,c))*((S(a,b,c))-a)*((S(a,b,c))-b)*((S(a,b,c))-c)))

#define TRI_PERI(a,b,c) (a+b+c)

#define SQR_AREA(x) (x*x)

#define SQR_PERI(x) (2*(x+x))

#define CIR_AREA(r) (PI*r*r)

#define CIR_PERI(r) (2*PI*r)

© 2020 Garbage Valuegarbage value logo