#if Directive


Syntax

The C preprocessor supports conditional compilation via the #if directive. The syntax of the #if directive is shown below.

conditional:
        if_part elif_parts else_part endif_line
 
if_part:
        if_line text
 
if_line:
        #if constant_expression
        #ifdef identifier
        #ifndef identifier

#elif_part:
        elif_line text
        elif_part elif_line text
 
elif_line:
        #elif constant_expression
 
else_part:
        else_line text
 
else_line:
        #else
 
endif_line:
        #endif

Example

#define A 1
#define B 2
#define C 3
 
#if !defined(A) || !defined(B)
  #define D 4
#elif defined B && !defined C
  #define E 5
#else
  #define F 6
#endif

In the above code fragment, the constant expression following the #if directive evaluates to false as does the constant expression following the #elif directive. Therefore the #else portion is executed, thereby causing the macro F to be defined. Note that the macros D and E are not defined.

Notes