#ifdef Include code when a macro is defined

#ifdef Include code when a macro is defined

#ifdef includes code when an identifier is defined as a macro. Even #define FLAG 0 satisfies the condition; use #if for values and #endif to close it.

CategoryPreprocessor directives
KindKeyword / Statement
C standardC89 / C90

📝 Syntax

#ifdef IDENTIFIER
    /* conditional code */
#endif

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

Feature enabled
⚠️ Warning Even after #define FLAG 0, #ifdef FLAG is true because FLAG is defined. Use #if FLAG when the replacement value must be tested.

📚 Related Tutorials

❓ FAQ

QAre #ifdef and #if defined equivalent?
A#ifdef NAME and #if defined(NAME) both test whether NAME is defined. The latter is easier to combine with &&, ||, and ! in compound conditions.
QDoes #ifdef pass when a macro is defined as zero?
AYes. #ifdef checks only whether the macro exists, so #define FLAG 0 still makes #ifdef FLAG true. Use #if FLAG when zero should mean disabled.
QCan one #ifdef directive test whether several macros are defined?
ANo. #ifdef accepts only one macro name. Use #if defined(A) && defined(B) for logical combinations, with || or ! as needed. Each test still checks only whether a macro exists, not its replacement value.
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%

🙏 帮我们做得更好

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

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