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.
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.
#define area(r) (r * r)
/* 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);