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

CategoryPreprocessor directives
KindKeyword / Statement
C standardC89 / 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 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏