#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.
| Category | Preprocessor directives |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / 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 preprocessor directives
📚 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.