#if#ifdef#ifndef#else#elif#elifdef#elifndef#endif (C23)(C23) | ||||
(C23) | ||||
(C99) | ||||
(C23)(C23) |
The preprocessor supports conditional compilation of parts of source file. This behavior is controlled by#if,#else,#elif,#ifdef,#ifndef,#elifdef,#elifndef(since C23), and#endif directives.
Contents |
#ifexpression | |||||||||
#ifdefidentifier | |||||||||
#ifndefidentifier | |||||||||
#elifexpression | |||||||||
#elifdefidentifier | (since C23) | ||||||||
#elifndefidentifier | (since C23) | ||||||||
#else | |||||||||
#endif | |||||||||
The conditional preprocessing block starts with#if,#ifdef or#ifndef directive, then optionally includes any number of#elif,#elifdef, or#elifndef(since C23) directives, then optionally includes at most one#else directive and is terminated with#endif directive. Any inner conditional preprocessing blocks are processed separately.
Each of#if,#ifdef,#ifndef,#elif,#elifdef,#elifndef(since C23), and#else directives control code block until first#elif,#elifdef,#elifndef(since C23),#else,#endif directive not belonging to any inner conditional preprocessing blocks.
#if,#ifdef and#ifndef directives test the specified condition (see below) and if it evaluates to true, compiles the controlled code block. In that case subsequent#else,#elifdef,#elifndef,(since C23) and#elif directives are ignored. Otherwise, if the specified condition evaluates false, the controlled code block is skipped and the subsequent#else,#elifdef,#elifndef,(since C23) or#elif directive (if any) is processed. If the subsequent directive is#else, the code block controlled by the#else directive is unconditionally compiled. Otherwise, the#elif,#elifdef, or#elifndef(since C23) directive acts as if it was#if directive: checks for condition, compiles or skips the controlled code block based on the result, and in the latter case processes subsequent#elif,#elifdef,#elifndef,(since C23) and#else directives. The conditional preprocessing block is terminated by#endif directive.
Theexpression is a constant expression, using onlyconstants and identifiers, defined using#define directive. Any identifier, which is not literal, non defined using#define directive, evaluates to0excepttrue which evaluates to1(since C23).
The expression may contain unary operators in formdefined identifier ordefined (identifier) which return1 if theidentifier was defined using#define directive and0 otherwise.In this context,__has_include,__has_embed and__has_c_attribute are treated as if they were the name of defined macros.(since C23) If theexpression evaluates to nonzero value, the controlled code block is included and skipped otherwise. If any used identifier is not a constant, it is replaced with0.
In context of a preprocessor directive, a | (since C23) |
Note: UntilDR 412,#ifcond1 ...#elifcond2 is different from#ifcond1 ...#else followed by#ifcond3 because ifcond1 is true, the second#if is skipped andcond3 does not need to be well-formed, while#elif'scond2 must be a valid expression. As ofDR 412,#elif that leads the skipped code block is also skipped.
Checks if the identifier wasdefined as a macro name.
#ifdef identifier is essentially equivalent to#if defined identifier.
#ifndef identifier is essentially equivalent to#if !defined identifier.
| (since C23) |
While#elifdef and#elifndef directives target C23, implementations may backport them to the older language modes as conforming extensions.
#define ABCD 2#include <stdio.h> int main(void){ #ifdef ABCDprintf("1: yes\n");#elseprintf("1: no\n");#endif #ifndef ABCDprintf("2: no1\n");#elif ABCD == 2printf("2: yes\n");#elseprintf("2: no2\n");#endif #if !defined(DCBA) && (ABCD < 2 * 4 - 3)printf("3: yes\n");#endif // C23 directives #elifdef/#elifndef#ifdef CPUprintf("4: no1\n");#elifdef GPUprintf("4: no2\n");#elifndef RAMprintf("4: yes\n");// selected in C23 mode, may be selected in pre-C23 mode#elseprintf("4: no3\n");// may be selected in pre-C23 mode#endif}
Possible output:
1: yes2: yes3: yes4: yes
The following behavior-changing defect reports were applied retroactively to previously published C standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| DR 412 | C89 | failed#elif's expression was required to be valid | failed#elif is skipped |
C++ documentation forConditional inclusion |