Main Content

MISRA C:2023 Rule 21.1

#define and #undef shall not be used on a reserved identifier or reserved macro name

Since R2024a

Description

Rule Definition

#define and #undef shall not be used on a reserved identifier or reserved macro name.

Rationale

Reserved identifiers and reserved macro names are intended for use by the implementation. Removing or changing the meaning of a reserved macro can result in undefined behavior. This rule applies to the following:

  • Identifiers or macro names beginning with an underscore

  • Identifiers in file scope described in the C Standard Library

  • Macro names described in the C Standard Library as being defined in a standard header

The rule checker can flag different identifiers or macros depending on the version of the C standard used in the analysis. See C standard version (-c-version). For instance, if you run a C99 analysis, the reserved identifiers and macros are defined in the ISO®/IEC 9899:1999 standard, Section 7, "Library".

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#undef __LINE__               /* Non-compliant - begins with _ */
#define _Guard_H 1            /* Non-compliant - begins with _ */
#undef _BUILTIN_sqrt        /* Non-compliant - implementation may
                               * use _BUILTIN_sqrt for other purposes,
                               * e.g. generating a sqrt instruction */
#define defined               /* Non-compliant - reserved identifier */
#define errno my_errno        /* Non-compliant - library identifier */
#define isneg(x) ( (x) < 0 )  /* Compliant - rule doesn't include  
                               * future library directions   */

Check Information

Group: Standard Libraries
Category: Required
AGC Category: Required

Version History

Introduced in R2024a