Main Content

MISRA C:2012 Rule 7.6

The small integer variants of the minimum-width integer constant macros shall not be used

Since R2025a

Description

Rule Definition

The small integer variants of the minimum-width integer constant macros shall not be used.1

Rationale

Minimum width integer constant macros of the form INTn_C(value) or UINTn_C(value) are implementation dependent. Depending on your compiler, it is possible that the expansion of these macros result in a full width integer type instead of the anticipated small integer type. In this code, the variable var might be a full-width signed integer in some implementations:

uint8_t var = UINT8_C( 100 );
Using the minimum-width integer constant macros can result in unexpected and implementation-dependent behavior. Avoid using these macros.

Polyspace Implementation

Polyspace® reports a violation if you use a minimum-width integer constant macro that represents an integer that is smaller than or equal to the size of int in your environment. For example, Polyspace reports a violation on these macros if your environment supports 32-bit integers:

  • INT8_C, UINT8_C

  • INT16_C, UINT16_C

If your environment supports 64-bit integers, Polyspace also reports the use of INT32_C and UINT32_C.

Use of these tokens in a #define statement is not a violation.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, the minimum-width integer macro UINT_C is used as the type name in the association list of a generic selection. In the function foo(), the generic selection is invoked using the argument UINT8_c(8). Depending on the compiler, the generic selection can invoke either of func_for_small_int() and func_for_int(). Polyspace reports a violation on the use of this macro.

You can redefine these macros using the fixed-width integer types from stdint.h. For example, this code redefines UINT16_C as a cast to uint16_t. The behavior of this code is not implementation dependent. Polyspace does not report a violation on the #define statement.


#include <stdint.h>

void func_for_small_int(uint8_t);
void func_for_int(int);

#define myGenericFunc(x) _Generic( (x), uint8_t: func_for_small_int, default: func_for_int )(x)

#define UINT16_C(m) (uint16_t)m

void foo(void)
{
	myGenericFunc(UINT8_C(8));       // Noncompliant
	uint8_t x = UINT16_C(5);         // Compliant
}

Check Information

Group: Literals and constants
Category: Required
AGC Category: Required

Version History

Introduced in R2025a


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.