#ifndef Include code when a macro is not defined
#ifndef Include code when a macro is not defined
#ifndef includes code when an identifier is not defined as a macro. Even #define FLAG 0 makes the condition false; it is often used for header guards.
| Category | Preprocessor directives |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
#ifndef IDENTIFIER
/* conditional code */
#endif
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
Feature available
💡 Tip An include guard typically uses #ifndef HEADER_NAME, #define HEADER_NAME, and #endif so that a header's contents are processed only once in a translation unit.
🏷️ Related preprocessor directives
📚 Related Tutorials
❓ FAQ
QWhat is the difference between #ifndef and #if !defined?
A#ifndef NAME is equivalent to #if !defined(NAME). The latter is usually more concise when combining several macro conditions.
QCan #ifndef test whether a macro's value is zero?
ANo. Once a macro is defined, #ifndef is false regardless of whether its replacement value is zero. Use #if for value tests, while accounting for the treatment of undefined identifiers in that expression.
QHow should I name an include guard macro?
ABuild a stable, distinctive identifier from the project, path, and header name so different headers cannot share a guard and skip each other's contents. Avoid reserved identifiers, such as names beginning with an underscore followed by an uppercase letter, and review the guard after renaming the header.