Main Content

MISRA C:2012 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.

This rule comes from MISRA C™: 2012 Amendment 3.

Rationale

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

Polyspace Implementation

The rule 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 that the name of the macro indicates. For example, the argument to UINT32_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( 0xDEADBEEF );  //Noncompliant
int32_t example4 = -INT32_C(5);            //Compliant
uint32_t example5 = UINT32_C(5UL);         //Noncompliant

In this code:

  • example1 is compliant because the argument is an unsuffixed decimal integer that can be represented as a signed 32-bit integer.

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

  • example3 is noncompliant because the integer value of the hex value 0xDEADBEEF is outside the limits of INT32_C.

  • example4 is compliant because the minus operator is outside of the macro.

  • example5 is noncompliant because 5UL is suffixed.

Check Information

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

Version History

Introduced in R2024a