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

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

🙏 帮我们做得更好

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

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