[previous] MISRA-C:2004  Rule  6.2:  (Required) [next] Signed and unsigned char type shall be used only for the storage and use of numeric values.
There are three distinct char types, (plain) char, signed char and unsigned char. Signed char and unsigned char shall be used for numeric data and plain char shall be used for character data. The signedness of the plain char type is implementation-defined and should not be relied upon.
Character values/data are character constants or string literals such as 'A', '5', '\n', "a".
Numeric values/data are numbers such as 0, 5, 23, \x10, -3.
Character sets map text characters onto numeric values. Character values are the "text".
The permissible operators on plain char types are the simple assignment operator (=), equality operators (==, !=) and explicit casts to integral types. Additionally, the second and third operands of the ternary conditional operator may both be of plain char type.

Example Code:


/* PRQA S 2982,2984,3205 ++ */
#include "misra.h"
#include "m2cmex.h"

static S8 test_0602a(void);
static U8 test_0602b(void);


extern S16 test_0602( void )
{
   u8a  = 1u;                  /* Correct usage of unsigned char for numeric   data */
   s8a  = 0;                   /* Correct usage of signed   char for numeric   data */
   pca  = 'u';                 /* Correct usage of plain    char for character data */

   s8a = pca;                  /* MISRA Violation */
   u8a = pca;                  /* MISRA Violation */

   s8a = pca + 1;              /* MISRA Violation */
   u8a = pca + 1;              /* MISRA Violation */

   s8a = test_0602a();
   u8a = test_0602b();

   return 0;
}

static S8 test_0602a(void)
{
    return pca;                /* MISRA Violation */
}

static U8 test_0602b(void)
{
    return pca;                /* MISRA Violation */
}


QAC messages that encompass this guideline:

4413 An expression of 'essentially character' type (%1s) is being converted to signed type, '%2s' on assignment.
4414 An expression of 'essentially character' type (%1s) is being converted to unsigned type, '%2s' on assignment.


Related rules:

Rule  10.1 The value of an expression of integer type shall not be implicitly converted to a different underlying type if: a) it is not a conversion to a wider integer type of the same signedness, or b) the expression is complex, or c) the expression is not constant and is a function argument, or d) the expression is not constant and is a return expression



(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