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

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

🙏 帮我们做得更好

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

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