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