Preprocessor Conditional Compilation
To use conditional, #ifdef
,#if
, #defined
,
#else
and #elif directives are used.
#ifdef Directive
#ifdef MACRO
// conditional codes
#endif
#if, #elif and #else Directive
#if expression
// conditional codes
#endif
Here, expression is an expression of integer type
(can be integers, characters, arithmetic
expression, macros, and so on).
The conditional codes are included in the program
only if the expression is evaluated to a non-zero
value.
The optional #else directive can be used with #if
directive.
#if expression
conditional codes if expression is non-zero
#else
conditional if expression is 0
#endif
#if expression
// conditional codes if expression is non-zero
#elif expression1
// conditional codes if expression is non-zero
#elif expression2
// conditional codes if expression is non-zero
#else
// conditional if all expressions are 0
#endif
#defined
#if defined BUFFER_SIZE && BUFFER_SIZE >= 2048
// codes
The special operator #defined is used to test
whether a certain macro is defined or not. It’s
often used with #if directive.