Main Content

MISRA C:2023 Rule 7.5

The argument of an integer constant macro shall have an appropriate form

Since R2024a

Description

Rule Definition

The argument of an integer constant macro shall have an appropriate form.

Rationale

Using an inappropriate form of an argument for an integer constant macro can lead to compilation errors or undefined behavior of the code due to scenarios such as unexpected substitutions, lack of type checking, and confusion of the compiler's assumptions and optimizations.

Polyspace Implementation

The checker reports a rule violation when using an inappropriate form of an argument of an integer constant macro. The form of an argument of an integer constant macro is considered appropriate if:

  • The argument is an unsuffixed integer literal such as a decimal, octal, or hexadecimal.

  • The value of the argument does not exceed the limits for the equivalent exact-width type the name of the macro indicates. For instance, the argument to INT32_C must be representable as an unsigned 32-bit value.

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

#include <stdint.h>

int32_t example1 = INT32_C(42);            //Compliant
int32_t example2 = INT32_C(1.e1);          //Noncompliant
int32_t example3 = INT32_C(-5);            //Noncompliant
int32_t example4 = -INT32_C(5);            //Compliant
uint32_t example5 = UINT32_C(5UL);         //Noncompliant

In this example:

  • example1 is compliant as the value of the argument meets the requirements to be appropriate.

  • example2 is noncompliant as 1.e1 is a floating-point literal, not an unsuffixed integer literal.

  • example3 is noncompliant as -5 is a negative signed value.

  • example4 is compliant as the minus operator it contains is outside of the macro.

  • example5 is noncompliant as 5UL is suffixed.

Check Information

Group: Literals and Constants
Category: Mandatory
AGC Category: Mandatory

Version History

Introduced in R2024a