[previous] MISRA C:2012  Dir-4.6:  (Advisory) [next] typedefs that indicate size and signedness should be used in place of the basic numerical types

Amplification:

The basic numerical types of char, short, int, long, long long (C99), float, double and long double (C99) should not be used, but specific-length typedefs should be used.

For C99, the types provided by <stdint.h> should be used. For C90, equivalent types should be defined and used.

A type must not be defined with a specific length unless the implemented type is actually of that length.

It is not necessary to use typedefs in the declaration of bit-fields.

For example, on a 32-bit C90 implementation the following definitions might be suitable:

typedef signed   char   int8_t;
typedef signed   short  int16_t;
typedef signed   int    int32_t;
typedef signed   long   int64_t;
typedef unsigned char   uint8_t;
typedef unsigned short  uint16_t;
typedef unsigned int    uint32_t;
typedef unsigned long   uint64_t;
typedef          float  float32_t;
typedef          double float64_t;
typedef long     double float128_t;

Exception:

  1. The basic numerical types may be used in a typedef to define a specific-length type.
  2. For function "main" an int may be used rather than the typedefs as a return type. Therefore int main (void) is permitted.
  3. For function "main" an int may be used rather than the typedefs for the input parameter argc.
  4. For function "main" a char may be used rather than the typedefs for the input parameter argv.

Therefore int main( int argc, char *argv[] ) is permitted (C99 Section 5.1.2.2.1).

Example Code:


#pragma PRQA_MESSAGES_OFF 2983

#include "misra.h"
#include "m3cmex.h"

typedef unsigned char UCHAR;

extern int16_t dir_0406( void )
{
   int8_t                 ys8a  = 1;
   int16_t                ys16a = 1;
   int32_t                ys32a;

   char                   xpca;                 /*      */

   signed char            xsca;                 /* 5209 */
   short                  xssa;                 /* 5209 */
   signed short           xssb;                 /* 5209 */
   short int              xssc;                 /* 5209 */
   signed short int       xssd;                 /* 5209 */
   int                    xsia;                 /* 5209 */
   signed                 xsib;                 /* 5209 */
   signed int             xsic;                 /* 5209 */
   long                   xsla;                 /* 5209 */
   signed long            xslb;                 /* 5209 */
   long int               xslc;                 /* 5209 */
   signed long int        xsld;                 /* 5209 */
   long long              xsxa;                 /* 5209 */
   long long int          xsxb;                 /* 5209 */
   signed long long       xsxc;                 /* 5209 */
   signed long long int   xsxd;                 /* 5209 */

   unsigned char          xuca;                 /* 5209 */
   unsigned short         xusa;                 /* 5209 */
   unsigned short int     xusb;                 /* 5209 */
   unsigned               xuia;                 /* 5209 */
   unsigned int           xuib;                 /* 5209 */
   unsigned long          xula;                 /* 5209 */
   unsigned long int      xulb;                 /* 5209 */
   unsigned long long     xuxa;                 /* 5209 */
   unsigned long long int xuxb;                 /* 5209 */

   float                  xfta;                 /* 5209 */
   double                 xdba;                 /* 5209 */
   long double            xlda;                 /* 5209 */

   struct bit_field
   {
      signed int   bit2 : 7;
      unsigned int bit3 : 2;
   };

   xfta = ( float )ys8a;                        /* 5209 */

   return 0;
}


QAC messages that encompass this guideline:

5209 Use of basic type '%s'.



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