Code that contains many macros becomes hard to understand. This is even worse when the set of defined macros is not stable, and you have to know at
each point what macros are defined. Therefore,
#undef can decrease the readability of macros.
However, well-disciplined use of
#undef can also improve readability, for instance when defining a macro with a limited scope: The
#defined, used a couple of times to reduce code duplication, then immediately
This rule raises an issue when a
#undef undefines a macro that was defined in another file. It will also raise an issue for an
#undef directive that tries to undefine a non-existing macro.
Noncompliant Code Example
#undef MY_HDR /* Noncompliant */
#define LEVEL(i) int const i = #i
- MISRA C:2004, 19.6 - #undef shall not be used.
- MISRA C++:2008, 16-0-3 - #undef shall not be used.
- MISRA C:2012, 20.5 - #undef should not be used