[previous] MISRA-C:2004  Rule  10.6:  (Required) [next] A "U" suffix shall be applied to all constants of unsigned type.
The type of an integer constant is a potential source of confusion, because it is dependent on a complex combination of factors including:

1. The magnitude of the constant

2. The implemented sizes of the integer types

3. The presence of any suffixes

4. The number base in which the value is expressed (i.e. decimal, octal or hexadecimal).

For example, the integer constant "40000" is of type int in a 32-bit environment but of type long in a 16-bit environment. The value 0x8000 is of type unsigned int in a 16-bit environment, but of type (signed) int in a 32-bit environment.

Note the following:

* Any value with a "U" suffix is of unsigned type

* An unsuffixed decimal value less than 231 is of signed type

But:

* An unsuffixed hexadecimal value greater than or equal to 215 may be of signed or unsigned type

* An unsuffixed decimal value greater than or equal to 231 may be of signed or unsigned type

Signedness of constants should be explicit. Consistent signedness is an important principle in constructing well formed expressions. If a constant is of an unsigned type, it is helpful to avoid ambiguity by applying a "U" suffix. When applied to larger values, the suffix may be redundant (in the sense that it does not influence the type of the constant); however its presence is a valuable contribution towards clarity.

Example Code:


/* PRQA S 0339,2982,3112,3205 ++ */

#include "misra.h"
#include "m2cmex.h"


/*******************************************************************
ISO 6.1.3.2 RULES:
==================
The type of an integer constant is the first of the corresponding list
in which its value can be represented.

UNSUFFIXED DECIMAL:             int, long, unsigned long
UNSUFFIXED OCTAL OR HEX:        int, unsigned int, long, unsigned long
SUFFIXED WITH U:                unsigned int, unsigned long
SUFFIXED WITH L:                long, unsigned long
SUFFIXED U and L:               unsigned long

The table below shows the type of an integer constant of various
magnitudes in different formats. Types shown are for a 16bit int.
------------------------------------------------------------------------
Decimal/Hexadecimal Value |UNSUFFXD|UNSUFFXD|SUFFIXED|SUFFIXED|SUFFIXED|
                          | DECIMAL| HEX/OCT|  WITH U|  WITH L| WITH UL|
------------------------------------------------------------------------
32767      / 0x7FFF       |  SI    |  SI    |  UI    |   SL   |   UL   |
32768      / 0x8000       |  SL    |  UI    |  UI    |   SL   |   UL   |
65536      / 0x10000      |  SL    |  SL    |  UL    |   SL   |   UL   |
2147483647 / 0x7FFFFFFF   |  SL    |  SL    |  UL    |   SL   |   UL   |
2147483648 / 0x80000000   |  UL    |  UL    |  UL    |   UL   |   UL   |
------------------------------------------------------------------------

The following examples assume that int is configured as 16 bits.
*******************************************************************/

extern S16 test_1006( void )
{
/* Decimal */

   2147483648;          /* MISRA Violation - unsigned long */
   4294967295;          /* MISRA Violation - unsigned long */
   2147483648L;         /* MISRA Violation - unsigned long */
   4294967295L;         /* MISRA Violation - unsigned long */

/* Hexadecimal */
   0x8000;              /* MISRA Violation - unsigned int  */
   0xFFFF;              /* MISRA Violation - unsigned int  */
   0x80000000;          /* MISRA Violation - unsigned long */
   0xFFFFFFFF;          /* MISRA Violation - unsigned long */
   0xFFFFFFFFL;         /* MISRA Violation - unsigned long */
   0x80000000L;         /* MISRA Violation - unsigned long */

/* Octal */
   0100000;             /* MISRA Violation - unsigned int  */
   0177777;             /* MISRA Violation - unsigned int  */
   020000000000;        /* MISRA Violation - unsigned long */
   037777777777;        /* MISRA Violation - unsigned long */
   020000000000;        /* MISRA Violation - unsigned long */
   037777777777L;       /* MISRA Violation - unsigned long */

   return 0;
}


QAC messages that encompass this guideline:

1281 Integer literal constant is of an unsigned type but does not include a "U" suffix.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents