Main Content

MISRA C:2012 Rule 7.2

A “u” or “U” suffix shall be applied to all integer constants that are represented in an unsigned type

Description

Rule Definition

A “u” or “U” suffix shall be applied to all integer constants that are represented in an unsigned type.

Rationale

The signedness of a constant is determined from:

  • Value of the constant.

  • Base of the constant: octal, decimal or hexadecimal.

  • Size of the various types.

  • Any suffixes used.

Unless you use a suffix u or U, another developer looking at your code cannot determine easily whether a constant is signed or unsigned.

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

const unsigned long C[] = {
	0x9421FFD0, /*Noncompliant*/
	0x5322E762, 
	0x80000000, /*Noncompliant*/
	0x7FFFFFFF,
	0x00000001,
	0x83241947, /*Noncompliant*/
	0x57112957,
	0x2640EA23
};

const unsigned long D[] = {
	0x9421FFD0U, /*Compliant*/
	0x80000000U, /*Compliant*/
	0x83241947U, /*Compliant*/
};

In this example, Polyspace® flags the unsigned members of C. For instance, 0x9421FFD0 is an unsigned number in a 32 bit environment because it exceeds the capacity of a signed integer. Because the unsigned number lacks the suffix u or U, Polyspace flags it. In D, the unsigned numbers use the suffix and are not flagged.

Check Information

Group: Literals and Constants
Category: Required
AGC Category: Readability

Version History

Introduced in R2014b