C Program to Find Area of Square using Macro

Previous Article
Next Article

This is a simple demonstration of a Macro in C language. This is program is written using Turbo C++ Compiler installed on a Windows XP 64-bit system. You can compile and run this program using an standard C compiler.

This program is intended for intermediate level learners who know how to write a complex C program.

Problem Definition

The program to compute area of a square use a C macro called “area (r) r * r “ where r is a parameter which can be replaced with any variable.

Macro code gets replaced directly where they appear within the program and then executed as regular C tokens.

The area(r) receives a variable value called “Length_of_Side” for a square and computes the area of a square.

Program Code

/* Program to find area of square using Macro in c */
#include <stdio.h>
#include <conio.h>
#define area(r) (r * r)
int length_of_side;
int area_of_square; clrscr();
/* Read the Length of Side of a Square */
printf("Enter the length of side in 'cm' of a Square:");
/* Calling Macro area(r) where r is length_of_side */
area_of_square = area(length_of_side);
printf("Area of Square = %d",area_of_square);
return 0;


Output - Area of Square Using C Macro
Output – Area of Square Using C Macro
Previous Article
Next Article