#if Select code with a preprocessing constant expression
#if Select code with a preprocessing constant expression
#if evaluates an integer constant expression: nonzero includes code. Undefined identifiers become 0; variables, function calls, and sizeof are unavailable.
| Category | Preprocessor directives |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
#if constant-expression
/* conditional code */
#endif
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
High level
ℹ️ Info The #if condition must be an integer constant expression evaluable during preprocessing. After macro expansion, identifiers that remain undefined are replaced by 0; use defined(NAME) to test macro existence explicitly.
🏷️ Related preprocessor directives
📚 Related Tutorials
❓ FAQ
QWhat is the difference between #if and a runtime if statement?
A#if decides which source lines participate in compilation and requires a constant expression. A runtime if statement is evaluated while the program runs and can use variables.
QCan #if evaluate an undefined macro?
AAn undefined identifier in a #if expression is treated as 0, but that does not define the macro as 0. Use defined(NAME), #ifdef, or #ifndef when undefined and zero-valued states must be distinguished.
QWhy can't #if use sizeof or runtime variables?
A#if is evaluated during preprocessing, before ordinary C semantic analysis, and no runtime object values exist then. Its condition therefore cannot depend on sizeof, function calls, or runtime variables. To select code by type size, use compiler predefined macros or constant macros generated during configuration.