Conditional Compilation - #if, #elif

Syntax

The #if and #elif directives compare the value of the expression to zero.

If the constant_expression evaluates to a nonzero value, the tokens that immediately follow the condition are passed on to the compiler.

If the expression evaluates to zero and the conditional compilation directive contains a preprocessor #elif directive, the source text located between the #elif and the next #elif or preprocessor #else directive is selected by the preprocessor to be passed on to the compiler. The #elif directive cannot appear after the preprocessor #else directive.

All macros are expanded, any defined() expressions are processed and all remaining identifiers are replaced with the token 0.

The expressions that are tested must be integer constant expressions with the following properties:

Note: If a macro is not defined, a value of 0 (zero) is assigned to it. In the following example, TEST must be a macro identifier:

#if TEST >= 1
   printf("i = %d\n", i);
   printf("array[i] = %d\n", array[i]);
#elif TEST < 0
   printf("array subscript out of bounds \n");
#endif

Another example of #if and #elif Directives



Preprocessor Directives - An Overview


Conditional Compilation - #ifdef
Conditional Compilation - #ifndef
Conditional Compilation - #endif
Example of Preprocessor Directives