What is macro?
Let's understand macro using following program:
/*c program for macro expansion*/
#include<stdio.h>
#define LENGTH 3
#define WIDTH 2
int main()
{
int r,c;
for(r=1; r<=LENGTH; r++)
{
for(c=1; c<=WIDTH; c++)
printf("%d%d",c,r);
printf("\n");
}
getch();
return 0;
}
The output of above program would be:
![]() |
| Figure: Screen shot of macro C program |
In above program, instead of writing 5 in the for loop we are writing it in the form of text as LENGTH and WIDTH, which have already been defined before main() through the statement.
#define LENGTH 3
#define WIDTH 2
This statement is called 'macro definition' or macro.
LENGTH and WIDTH in the above program are often called 'macro templates' , whereas 5 and 3 are called their corresponding 'macro expansions'.
What is reason of using macro in the program?
