Thursday, 31 August 2017

Preprocessor Directive

A pre-processor directory is a set of text which is start with # symbol. When C source code compile then process of compilation get start from pre-processor directory. It means all pre-processor directories compile before actual compilation.

Preprocessor Directive C Language

Example
  • Header files
  • Macro
  • Conditional Compilation statements

why do we need pre-processor directives


We use pre-processor directives for a variety of things:

1. Macro replacement: To replace a particular text pattern with another one. This achieves one of several things - replace a text pattern with a constant or replace a text pattern with a parameterized expression - #define

2. Define a variable that the per processor can see - #define

3. Includes source code from external files - #include

4. Conditionally exclude a block of code from the original source code that will be subsequently processed by the compiler. This is done using previously defined pre processor variables - #ifdef or #ifndef

Various Pre-processor Directive


Directive
Description
#define
Substitutes a preprocessor macro.
#include
Inserts a particular header from another file.
#undef
Undefines a preprocessor macro.
#ifdef
Returns true if this macro is defined.
#ifndef
Returns true if this macro is not defined.
#if
Tests if a compile time condition is true.
#else
The alternative for #if.
#elif
#else and #if in one statement.
#endif
Ends preprocessor conditional.
#error
Prints error message on stderr.
#pragma
Issues special commands to the compiler, using a standardized 


No comments:

Post a Comment