Macro Expansion


There are several steps involved form the stage of writing a program to the stage of getting it executed. The combination of these steps is called build process. When a C program is compiled it is passed to through another program called preprocessor. The preprocessor offers several features called preprocessor directives. Each directive starts with a symbol #. The directives can be placed anywhere in the program but are usually used in the beginning of a program when the function gets defined. One of the preprocessor directives is the Macro expansion.

Macro expansion example:


#define LOW 25

Void main()

{ int I;

for (i=1;i<=LOW;i++)



In this program we are not using for loop for 25 times instead we are using a keyword as Low which is already defined in the main().

#define LOW 25

This statement is called a macro definition or often referred only as Macro. It helps the preprocessor to replace every occurrence of LOW on the program by 25.

#define DA 1.5

LOW and DA used above are often called Macro Templates whereas 25, 1.5 are referred to as Macro expansions.

When we compile a program before the source code passes to the compiler it is examined by the C preprocessor for any macro definations. When it sees a #define statement, it goes through the entire program in search for a macro template. Wherever it finds one , it replaces it with a macro expansion. Only after this is the program passed on to the compliler.

In C programming it is mandatory to use capital letter for Macro template. This makes it easy for programmers to pick out all the micro templates when reading through the program.

A macro template and a macro expansion are always separated by blanks or tabs. A macro definition is never to be terminated by a semicolon.

Why do you actually use a macro expansion?

1.)                                               Why should be use a template such as DA instead of 1.5. both of them are easily understandable but sometimes some terms are way complex, we certainly need a template to replace those. For example : x1B[2J is a command which is used for clearing screen so it is easy for us if we use a template called CLEARSCREEN rather than x1B[2J on the screen . It is easier to remember a template name rather than the actual word/code.

2.)                                               If you define a value such as a constant in the program and replace that with a template. In future if you have to change the constant value all you have to do is to replace the constant once rather than going in through the entire program and replacing each constant. We would just require to change the constant value in the starting which was defined with the Macro template.

Macros that have been defined now are simple macros. We also have many more macros such as macros with arguments


#include AREA(x) (3.14 *r*r)

Void main()

{ float r1=6.25,r2=3.9;



Print(\n area of circles=%f,a,b)

Macros with functions

If we take the above example in consideration we could differentiate why we use functions instead of macros. If we use a macro hundred times in a program, the macro expansion goes into our source code at hundread different places, thereby increasing the memory size whereas when we use functions even though it is called a thousand times in the same program it will occupy the same space.

Points to remember

a.)                                               Dont leave blank spaces in between the macro template and its arguments

b.)                                               The entire macro expansion should be enclosed in parentheses.

c.)                                                Macros can be split into many lines using a back slash (\)

d.)                                               In case of a debugging issue check the source code where the macros was declared.

Understand Computer Science better with these Q&A

More Q&A
Submit Your Questions Here!
Copy and paste your question here...

Attach Files

Top Computer Science Online Experts