Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Conditional inclusion

      From cppreference.com
      <c‎ |preprocessor
       
       
       
      Preprocessor
      #if#ifdef#ifndef#else#elif#elifdef#elifndef#endif
      (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

      [edit]Syntax

      #ifexpression
      #ifdefidentifier
      #ifndefidentifier
      #elifexpression
      #elifdefidentifier(since C23)
      #elifndefidentifier(since C23)
      #else
      #endif

      [edit]Explanation

      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.

      [edit]Conditional evaluation

      [edit]#if, #elif

      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__has_c_attribute expression detects whether a given attribute token is supported and its supported version. SeeAttribute testing.

      (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.

      [edit]Combined directives

      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.

      #elifdef identifier is essentially equivalent to#elif defined identifier.

      #elifndef identifier is essentially equivalent to#elif !defined identifier.

      (since C23)

      [edit]Notes

      While#elifdef and#elifndef directives target C23, implementations may backport them to the older language modes as conforming extensions.

      [edit]Example

      Run this code
      #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

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C standards.

      DRApplied toBehavior as publishedCorrect behavior
      DR 412C89failed#elif's expression was required to be validfailed#elif is skipped

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.10.1 Conditional inclusion (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.10.1 Conditional inclusion (p: 118-119)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.10.1 Conditional inclusion (p: 162-164)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.10.1 Conditional inclusion (p: 147-149)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.8.1 Conditional inclusion

      [edit]See also

      C++ documentation forConditional inclusion
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/preprocessor/conditional&oldid=154407"

      [8]ページ先頭

      ©2009-2025 Movatter.jp