Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Preprocessor, Tools and Concepts

Conditional compilation: #if, #ifdef, #ifndef

Reference for C++ conditional compilation: #if, #elif, #else, #endif, #ifdef, #ifndef, defined(), #elifdef, #error and #warning, with practical examples.

The conditional directives let the preprocessor include or skip whole regions of source code depending on macros and constant expressions. Code in a skipped region is not compiled at all: it only has to consist of valid preprocessing tokens, not valid C++. This is how a single code base adapts to different platforms, compilers, build types and optional features.

#if and #elif test an integer constant expression (macros are expanded first, and any leftover identifier counts as 0). #ifdef NAME and #ifndef NAME test only whether a macro is defined. Every conditional block ends with #endif. Since C++23, #elifdef and #elifndef are the matching shortcuts for #elif defined(...).

C++
#if defined(DEBUG) && DEBUG > 1
    // verbose logging
#elif defined(DEBUG)
    // light logging
#else
    // nothing
#endif

Try it

C++ #if, #elif, #else and defined()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Skipped code only needs valid tokens, and #elifdef

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Fail-fast checks use #error (stops compilation) and #warning (C++23, a message; a common extension earlier). They are read-only here because they would stop or clutter the build:

C++
#if !defined(__cplusplus) || __cplusplus < 202002L
    #error "This project needs C++20 or newer"
#endif

Syntax

C++
#if constant-expression
#elif constant-expression
#else
#endif

#ifdef identifier        // same as #if defined(identifier)
#ifndef identifier       // same as #if !defined(identifier)
#elifdef identifier      // C++23
#elifndef identifier     // C++23

#error message
#warning message         // C++23
Piece Description
defined(X) or defined X 1 if macro X is defined, else 0. Only valid inside #if/#elif.
__has_include(h) 1 if the header exists.
__has_cpp_attribute(a) Non-zero if the attribute is supported (the value is the year/month it was added).
Operators Integer arithmetic, comparisons, &&, `

Errors: every #if/#ifdef needs a matching #endif in the same file, or compilation fails. Floating-point or string expressions in #if are errors.

Usage notes

  • #if versus #ifdef: #ifdef X is true when X is defined even as #define X 0; #if X would then be false. Pick one convention per macro: "defined or not" (feature flags) or "always defined, value 0/1" (so typos can be caught with -Wundef).
  • Use defined with care. Writing #if defined(X) && X > 2 avoids relying on undefined names evaluating to 0.
  • #if is not if. It cannot see variables, constexpr values or sizeof. For compile-time decisions based on C++ entities use if constexpr or static_assert; it keeps all branches type-checked.
  • Prefer if constexpr over #if when the choice depends on a template parameter or a type property.
  • Indent directives (# if) for nested blocks to keep them readable, and comment the closing line: #endif // USE_COLOR.
  • Too many variants make code untestable: each combination must compile. Keep the number of switches small and test all builds in CI.
  • Platform macros such as _WIN32, __linux__, __APPLE__, __GNUC__, _MSC_VER are compiler-defined; wrap them in your own named macro once, then use that.
  • NDEBUG is the standard macro that disables assert. Build types set it with -DNDEBUG.
  • Include guards use this mechanism: #ifndef FOO_H ... #endif.
  • Shadowed code rots. Code under a rarely enabled #if often stops compiling. Prefer a runtime or if constexpr switch where practical.