[previous] MISRA-C:2004  Rule  6.1:  (Required) [next] The plain char type shall be used only for the storage and use of character 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.

The only permissible operators on plain char types are assignment and equality operators ( =, ==, != ).

Example Code:


/* PRQA S 2982,3112,3197,3203,3205,3206 ++ */
#include "misra.h"
#include "m2cmex.h"

static void test_0601a(PC *p);
static PC test_0601b(void);

enum E {ONE, TWO};

extern S16 test_0601( void )
{
   PC  c[] = "string";            /* Correct usage of char for character */

   pcr = test_0601b();

   pcr = u8a;                     /* MISRA Violation */
   pcr = s8a;                     /* MISRA Violation */
   pcr = s16a;                    /* MISRA Violation */
   pcr = u16a;                    /* MISRA Violation */
   pcr = s32a;                    /* MISRA Violation */
   pcr = u32a;                    /* MISRA Violation */

   pcr = pca + pcb;               /* MISRA Violation */
   pcr = s8a - pca;               /* MISRA Violation */
   pcr = u8a - pca;               /* MISRA Violation */
   f32r = f32a - pca;             /* MISRA Violation */
   f32r = f32a + pca;             /* MISRA Violation */

   pcr = (s8a != 0);              /* MISRA Violation */
   pcr = (s8a < 0);               /* MISRA Violation */

   {
      enum E e = ONE;
      pcr = e;                    /* MISRA Violation */
      pcr = TWO;                  /* MISRA Violation */
   }

   pcr = (f32a != 0.f);           /* MISRA Violation */
   pcr = (f32a < 0.f);            /* MISRA Violation */

   pcr = f32a;                    /* MISRA Violation */
   pca = (1.0f / 3.0f);           /* MISRA Violation */

   {
      S16 asi[10];
      asi[pca] = 0;               /* MISRA Violation */
      pca[asi] = 0;               /* MISRA Violation */
   }

   {
      pca + pcb;   /* OK: essential type of result is char */
      pca - pcb;   /* OK: essential type of result is char */
      pca - '0';   /* OK: essential type of result is int */
      
      pca * pcb;                  /* MISRA Violation */
      +pca;                       /* MISRA Violation */
      ++pca;                      /* MISRA Violation */
   }

   {
      pca & 1u;                   /* MISRA Violation */
      pca | 1u;                   /* MISRA Violation */
      ~pca;                       /* MISRA Violation */
      pca >> 1u;                  /* MISRA Violation */
      1u << pca;                  /* MISRA Violation */
   }

   {
      pca && pcb;                 /* MISRA Violation */
      pca || ! pca;               /* MISRA Violation */
   }

   s16r = pca ? 0 : 1;            /* MISRA Violation */

   pcr = u8a + u8b;               /* MISRA Violation */
   pcr = s8a + s8b;               /* MISRA Violation */

   pcr = 65;                      /* MISRA Violation */
   pcr = 65U;                     /* MISRA Violation */
   pcr = 3.14F;                   /* MISRA Violation */


   test_0601a(c);

   return 0;
}

static void test_0601a(PC *p)
{
}

static PC test_0601b(void)
{
    switch (s8a)
    {
    case 1: return(u8a);          /* MISRA Violation */
    case 2: return(s8a);          /* MISRA Violation */
    case 3: return(s16a);         /* MISRA Violation */
    case 4: return(u16a);         /* MISRA Violation */
    case 5: return(s32a);         /* MISRA Violation */
    case 6: return(u32a);         /* MISRA Violation */
    default: return('\0');
    }
}


QAC messages that encompass this guideline:

1810 An operand of 'essentially character' type is being added to another operand of 'essentially character' type.
1811 An operand of 'essentially character' type is being subtracted from an operand of 'essentially signed' type.
1812 An operand of 'essentially character' type is being subtracted from an operand of 'essentially unsigned' type.
1813 An operand of 'essentially character' type is being balanced with an operand of 'essentially floating' type in this arithmetic operation.
4401 An expression of 'essentially Boolean' type (%1s) is being converted to character type, '%2s' on assignment.
4421 An expression of 'essentially enum' type (%1s) is being converted to character type, '%2s' on assignment.
4431 An expression of 'essentially signed' type (%1s) is being converted to character type, '%2s' on assignment.
4441 An expression of 'essentially unsigned' type (%1s) is being converted to character type, '%2s' on assignment.
4451 An expression of 'essentially floating' type (%1s) is being converted to character type, '%2s' on assignment.
4510 An expression of 'essentially character' type (%1s) is being used as an array subscript.
4511 An expression of 'essentially character' type (%1s) is being used as the %2s operand of this arithmetic operator (%3s).
4512 An expression of 'essentially character' type (%1s) is being used as the %2s operand of this bitwise operator (%3s).
4513 An expression of 'essentially character' type (%1s) is being used as the left-hand operand of this shift operator (%2s).
4514 An expression of 'essentially character' type (%1s) is being used as the right-hand operand of this shift operator (%2s).
4517 An expression of 'essentially character' type (%1s) is being used as the operand of this increment/decrement operator (%2s).
4518 An expression of 'essentially character' type (%1s) is being used as the %2s operand of this logical operator (%3s).
4519 An expression of 'essentially character' type (%1s) is being used as the first operand of this conditional operator (%2s).


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