Preprocessor in 'C'


Preprocessor  :

  • As the name suggests a processor which performs pre-processing or some processing on a program before submitting it for actual compilation is called preprocessor. 
  • For example inclusion of header files in a program.

Role of Pre-processor :

  • Program written in C language is given to preprocessor. 
  • Preprocessor checks of preprocessor directives if any present in given program. It performs specific actions according to directive instructions and submit modified code to the compiler. 
  • Compiler then compiles it and produces object code which is then executed with the help of linker.

Format of preprocessor directive :

  • Preprocessor directive is an instruction given to preprocessor to perform certain task.
  • All preprocessor directives begins with # followed by identifier and directive name respectively. 
  • For example #include ,#define ,#undef etc. In C preprocessor allows us to do inclusion of header files, expansion of macros, compilation by condition and line control.


File inclusion directive (#include) :

  • #include is basically used to include different header files in our program.

Syntax :
#include<filename>
           or
#include "filename"

  • Both of above have same purpose.
  • Difference between them is that when we use first syntax then preprocessor search for particular file in systems directory whereas if we use second syntax then preprocessor first search for file in current working directory followed by searching in systems directory.

Macro substitution directive, argumented and nested macro :

  • Macro substitution is a process of replacing or substituting macro name by its definition also called macro expansion. 
  • Macro can be simple macro or argumented macro. 
  • Simple macro has 
     syntax : #define identifier value

  • In simple macro substitution preprocessor just replaces occurence of identifier by its value. 
  • Simple macros are mostly used to define widely used constants in a program. 
  • For example #define PI 3.142 
  • Here everyoccurence of PI will get replaced by 3.142.
  • Consider following program to illustrate simple macro

#include<stdio.h>
#define PI 3.142
int main()
{
 int r;
 float area;
 printf("\n Enter radius of circle : ");
 scanf("%d",&r);
 //using simple macro
 area = 2*PI*r;
 printf("\n Area of circle is %f",area);
 return 0;
}

Output :
 Enter radius of circle : 4
 Area of circle is 25.136000

  • Macro having argument in its definition is called argumented macro. 
  • Syntax:
       #define identifier(arg1,arg2,...) string

  • string can be anything which should be returned as output or will be replaced by identifier. 
  • For example : #define square(n) n*n;
  • It will replace all occurrences of sqare(n) in a program by n*n. 
  • Consider following program to illustrate argumented macro

#include<stdio.h>
#define square(n) n*n
int main()
{
 int m;
 printf("\n Enter any number : ");
 scanf("%d",&m);
 printf("\n Square of %d is %d",m,square(m));
 return 0;
}

Output :
 Enter any number : 5
 Square of 5 is 25

  • A macro having name of another macro in its definition is called nested macro
  • For example consider following program 

#include<stdio.h>
#define PI 3.142
#define circle_area(r) 2*PI*r
int main()
{
 int rad;
 float area;
 printf("\n Enter radius : ");
 scanf("%d",&rad);
 area = circle_area(rad);
 printf("\n Area of circle = %f",area);
 return 0;
}

Output :
 Enter radius : 6
 Area of circle = 37.703999

Note : macro circle_area is having macro PI nested with in it.

Macro versus Function :



Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.