#elif Add sequentially tested preprocessing branches
#elif Add sequentially tested preprocessing branches
#elif adds branches tested in order. Only the first true branch is included; the rest are skipped. A group may have many #elif directives and one #endif.
| Category | Preprocessor directives |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
#if first-expression
/* first branch */
#elif second-expression
/* second branch */
#else
/* fallback branch */
#endif
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
Two
💡 Tip A conditional group may contain several #elif directives between its opening condition and optional #else. Once a branch is selected, every later branch is skipped.
🏷️ Related preprocessor directives
📚 Related Tutorials
❓ FAQ
QHow many #elif directives can one conditional group contain?
AUse as many #elif directives as needed, with one opening conditional directive (#if, #ifdef, or #ifndef), at most one #else, and one matching #endif to close the group.
QDoes #elif work like a runtime else if?
ABoth select branches in order, but #elif uses constant expressions during preprocessing to choose code for compilation, while a runtime else if chooses an execution path while the program runs.
QAre later #elif conditions evaluated after an earlier branch is true?
ANo. The preprocessor selects the first true branch and skips the rest of the group; the final #else is used only if no earlier branch was selected. Regardless of evaluation, every branch must still maintain correctly paired conditional directives.