Preprocessor directives


Preprocessor processes source program before it is passed to compiler.Preprocessor offers various preprocessor directives.They start with a # symbol.

Below are some of the preprocessor directives.


#include

This can include both the system and user header files in another file.

eg. #include<stdio.h>

This will subsitute the contents of header stdio.h in your program at the preprocessing stage.


#define

This defines a macro.

eg.   #define PI 3.1415

Wherever macro PI would be encountered in the program,this value would be substituted.


eg.  #define AREA(r) PI * r * r

This paramaterized macro would take value of r and expand to it depending on the value.

Suppose the preprocessor encounters AREA(5) then 3.1415 * 5 * 5 would be 
subsituted for it.


#undef

This undefines a macro.

eg. #undef PI



#ifdef
#ifdef MNAME

statements

#endif

Only if the macro MNAME is defined , the statements will be included in the output of the preprocessor. 

Other directives include #ifndef,#pragma,#error,etc.

No comments:

Post a Comment