Computer Programming | Education

Macro Expansion Directives In C Preprocessor



The C pre-processor is a program that process source program or source code before it is passed to the compiler.

It works on the source code and creates expanded source code. It is this expanded source code that is executed by the compiler. In C programming the pre-processor offers several features called pre-processor directives. One of them is macro expansion directives, and this article will discuss about them.

MACRO EXPANSION DIRECTIVES

It uses #define directive. Using #define, one can produce more efficient and more easily understandable programs.

General syntax:

#define<macro_name><expansion> //definition

Example:

#include<stdio.h>
#define PI 3.1415
int main()
{
  float r=5.36;
  float area;
  area= PI*r*r;
  printf("Area of circle=%d\n", area);
  return 0;
}

Here #define PI 3.1415 is called as macro definition . Also PI is macro_name whereas, 3.1415 is called as macro expression.

You can change the values of the constants at all the places by making change in the #define directive.

How does the above program works?

Before this source code passes to the compiler , it is firstly examined by the C Preprocessor . As it sees #define directive it searches the macro name in the entire program . as it finds PI (macro_name) it replace it with given macro expansion (3.1415). After all this procedure program is compiled by the compiler .

Note:

  • Macro expansions or macro definition are always written before the main( ) function.
  • Macro templates (or macro_name) and it’s macro expansion are separated by blanks or tabs.
  • Space between # and define is optional
  • Macro definition is never to be terminated by a semicolon.

#define Directive is most popularly used in following two ways:

1. Simple Macros

(i) This #define directive is many a time used to define operators as shown below

#include<stdio.h>
#define AND &&
#define OR ||
int main( )
{
  int s=1, x=3 , y=50;
  if ((s<=5) AND (x<=10 OR y<30))
      printf( "You are lucky\n");
  else
      printf( "Try your luck next time\n"); 
  return 0;
}

(ii) This #define directive could be used even to replace a condition

#include<stdio.h>
#define AND &&
#define CHOICE( x>30 AND x<=55)
int main( )
{
 int x;
 printf(" enter number=");
 scanf("%d",&x);
 if(CHOICE)
     printf("correct choice\n");
 else
     printf("Wrong choice\n");
return 0;
}

(iii) A #define directive could be used to replace even an entire C statement

#include<stdio.h>
#define ACTIVE printf("The living Virus\n");
int main( )
{
  char sign;
  if (sign=='Y')
     ACTIVE 
  else
    printf("Safe as yet\n");
  return 0;
}

2. Macros with arguments

Macros have arguments just like functions . And they work in similar way.

Example:

#include<stdio.h> 
#define AREA(l,b) (l*b)  //macro with parameter
int main( )
{
  int l=10, b=5, area;
  area= AREA(l,b);
  printf("Area of rectangle is: %d",area);
  return 0;
}

here statement AREA(l,b) is equivalent to : (l * b)

OUTPUT of above Program: 
Area of rectangle is:50.00

Let’s see how this above program works . When the above source code passes through the pre-processor, what the compiler gets to work on will be this:

#include<stdio.h>
int main( )
{
int l=10, b=5, area;
area=l*b;
printf("Area of rectangle is: %d", area);
return 0;
}

Note:

  • Don’t leave a blank between the macro template and its argument while defining the macro. For example, there should be no blank between AREA and (l . b) in the definition, #define AREA( l, b ) (l*b) .
  • The entire macro expansion should be enclosed within parenthesis.

Here is an example ,what would happen if we fail to enclose the macro expansion within parenthesis

#include<stdio.h>
#define SQUARE(n) n*n
int main()
{
  int j;
  j=64/SQUARE(4);
  printf("j=%d\n",j);
  return 0;
}
The OUTPUT of the above program would be: j=64
whereas, the OUTPUT should be j=4
So, What went wrong? Basically the macro was expanded into 
j=64/4*4
which resulted 64
  • Macros can be split into multiple lines, with a ‘\’ (back slash) present at the end of each line. Example;
#define HLINE for(i=0;i<80;i++)\
                 printf("%c",196);
#define VLINE(X,Y) {\
                     gotoxy(X,Y);\
                     printf("%c",180);\
                     }

Macros Verses Functions

In this we will discuss the difference between macros and functions. As we know , the way we can use macro to calculate area of rectangle , in the same we can use function to calculate the area of rectangle . Though macro calls are like function calls, but they are not really same things.

  • In a macro call, the preprocessor replaces the macro template with its macro expansion. while in a function call the control is passed to a function along with certain arguments .
  • Macros sometimes run faster but increases the program size, whereas functions make the program smaller and compact.
  • Macros are well known for consuming space or increasing the size. If we use macros numbers of times in a program, the macro expansion goes into source code at numbers different places, thus increasing the program size . on the other hand if we call function numbers of time, it would still take the same amount of space in the program.
  • Macros are faster than functions, like passing arguments to a function and getting back the returned value it takes time and would further slow down the program. but this gets avoided with macros since they are already expanded and placed in the source code before compilation.

Thank you for reading this article,

Team BloggerBoy




3 Comments

  1. Good day! I could have sworn I’ve visited your blog before but after browsing through many of the posts I realized it’s new to me. Anyhow, I’m definitely happy I discovered it and I’ll be bookmarking it and checking back regularly!

Leave a Reply