Pre Processor Directives
Pre Processor Directives
C
Definition
A preprocessor is a program that
processes our source program before it
is passed to the compiler.
The preprocessor works on the source
code and creates “expanded source
code”.
The preprocessor offers several features
called preprocessor directives.
Each of theses preprocessor directives
begin with a # symbol.
The directives can be placed anywhere
in a program but most often placed at the
beginning of the program,before the
function definition.
Preprocessor directives are executed before
compilation.
The various preprocessor directives
are as follows:
1)Macro Expansion (#define)
II)File Inclusion(#include)
III) Conditional Compilation(#ifdef -#endif)
IV)Miscellaneous Directives
(#undef)
Macro Expansion
A “macro” is a fragment of code which
has been given a name.
Wherever the name is used,it is
replaced by the contents of the macro.
There are two kinds of macros which
are as follows:
I) Object-like macros resemble data
objects
II)Function-like macros resemble function
calls.
Object –like Macros
The object-like macro is an identifier
that is replaced by value.
It is widely used to represent numeric
constants.
It is called object-like because it looks
like a data object in code that uses it.
Syntax:
#define identifier replacement-text
Examples
#define x 4 x=identifier 4=replacement-text
void main()
{
int a;
a=x;
}
Example:
Conditional Compilation
These allow us to include certain
portion of the code depending upon
the output of constant expression.
The #ifdef preprocessor directive
checks if macro is defined by #define.
If yes, it executes the code otherwise
#else code is executed, if present.
Syntax:
Example:
#ifndef directive
The #ifndef preprocessor directive
checks if macro is not defined by
#define.
If yes, it executes the code otherwise
#else code is executed, if present.
Syntax:
Example: