Preprocessor
         Are the set of programs which processes programs before it passed to compiler. These are also called as pre-processor directives. 

- They always starts with #. 

- No semicolon is placed at the end

- Placed before main()


Usually pre-processors are classified into 4 categories

- Macro Expansion

- File inclusion

- Conditional compilation

- Miscellaneous directives


Diagram depicting steps of  c program execution :



Macro Expansion :


#define pi 3.142
main ()
{
int r;

float area;

printf("\nEnter the value for r ");
scanf("%d",&r);

area=pi * r * r;

printf("\nArea = %f",area);

}

#define pi 3.142


The above statement usually referred as called macro definition or simply macro. At the time pre-processing the preprocessor replace all occurrences of pi with the value 3.142 .

What actually happens when program is compiled?

We will consider the above example,When we issue compile command, before the code is sent to the compiler, program is  scan for any pre-processor directive. If any like # define are present then it checks the whole program for macro templates and replaces them with macros expansion, After this process program is passed to compiler

Macro templates are case sensitive ?


Macro templates are not case sensitive. Any can case can be used but at the time of defining macro template what case is used same must be followed throughout the program.

Macro templates does not terminate with semicolon(;) like the following line is invalid,

#define pi 3.142;

Assignment operator is not used only space used to separate the macro name and value.

Example:

#define pi=3.142

/* above one is invalid because assignment operator(=) is not allowed*/


One more important reason for using macro definition in the large program is, a constant value 3.142 repetitively used in more than one location of a program. Later if this value has to be modified than in all its occurrences have to be changed, This consumes more time and if any occurrence is missed than it would create inaccuracy in your final output. Hence if macro definition is created and used in program than to make changes to constant value just change in # define and it will get affected throughout the program.

#define can also be used for defining operators.

An expression like,

if((condition1 && condition2) || (condition3 && condition4))

in the above expression && (logical AND operator and || (logical OR ) operator can be replaced by defining macro for operators, it is shown in below statements,

#define AND &&

#define OR ||

the above two line shows that AND, OR defined for operator &&, || respectively because of this above logical expression can be rewritten as follows,

if((condition1 AND condition2) OR (condition3 AND condition4))

A whole condition can be replaced by #define, see below example,

#define OR ||
#define CHECK (a<=0 OR a>100)
main()
{
if(CHECK)
   printf("invalid value does not lies in the given range");
else
   printf("valid value lies in the given range");
}

.. continued (other types of pre-processor directive )

Comments