#undef Remove a macro definition

#undef Remove a macro definition

#undef removes a macro, so #ifdef treats it as undefined and no replacement occurs. It is permitted even if the macro name is currently undefined.

CategoryPreprocessor directives
KindKeyword / Statement
C standardC89 / C90

📝 Syntax

#undef IDENTIFIER

▶ Example

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

Output:

MODE = 2
⚠️ Warning After #undef, do not use the old macro name in ordinary C code unless it has been redefined or otherwise legally declared. The example redefines MODE after #undef, so its final replacement is 2.
ℹ️ Info Applying #undef to a name that is not currently defined has no effect and is not an error.

📚 Related Tutorials

❓ FAQ

QWhat is the difference between #undef and defining a macro as zero?
A#undef FLAG removes the macro, making #ifdef FLAG false. #define FLAG 0 keeps it defined, so #ifdef FLAG remains true while #if FLAG is false.
QWhy is #undef often used before changing a macro definition?
ARemoving the old definition first makes the replacement explicit and avoids diagnostics caused by an incompatible redefinition of the same macro.
QDoes #undef report an error for a macro that is not defined?
ANo. The standard says the directive has no effect in that case. It is not a way to test whether a macro exists, so a misspelling can pass unnoticed. Use #ifdef, #ifndef, or defined(NAME) when the code must distinguish whether a macro is defined.
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%

🙏 帮我们做得更好

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

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