[previous] MISRA C:2012  Rule-10.2:  (Required) [next] Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations

Amplification:

The appropriate uses are:

  1. For the + operator, one operand shall have essentially character type and the other shall have essentially signed type or essentially unsigned type. The result of the operation has essentially character type.
  2. For the - operator, the first operand shall have essentially character type and the second shall have essentially signed type, essentially unsigned type or essentially character type. If both operands have essentially character type then the result has the standard type (usually int in this case) else the result has essentially character type.

Example Code:


#pragma PRQA_MESSAGES_OFF 3112

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

#ifndef PEXP
#define PEXP pca
#endif

extern int16_t rule_1002( void )
{
/************/
/* ADDITION */
/************/

   PEXP + bla;                   /*      4501 */

   PEXP + 'a';                   /* 1810      */

   PEXP + n1a;                   /*      4521 */

   PEXP + sca;                   /*           */
   PEXP + ssa;                   /*           */
   PEXP + sia;                   /*           */
   PEXP + sla;                   /*           */
   PEXP + sxa;                   /*           */

   PEXP + uca;                   /*           */
   PEXP + usa;                   /*           */
   PEXP + uia;                   /*           */
   PEXP + ula;                   /*           */
   PEXP + uxa;                   /*           */

   PEXP + fta;                   /* 1813      */
   PEXP + dba;                   /* 1813      */
   PEXP + lda;                   /* 1813      */



   bla + PEXP;                   /*      4501 */

   'a' + PEXP;                   /* 1810      */

   n1a + PEXP;                   /*      4521 */

   sca + PEXP;                   /*           */
   ssa + PEXP;                   /*           */
   sia + PEXP;                   /*           */
   sla + PEXP;                   /*           */
   sxa + PEXP;                   /*           */

   uca + PEXP;                   /*           */
   usa + PEXP;                   /*           */
   uia + PEXP;                   /*           */
   ula + PEXP;                   /*           */
   uxa + PEXP;                   /*           */

   fta + PEXP;                   /* 1813      */
   dba + PEXP;                   /* 1813      */
   lda + PEXP;                   /* 1813      */

/***************/
/* SUBTRACTION */
/***************/

   PEXP - bla;                   /*      4501 */

   PEXP - 'a';                   /*           */

   PEXP - n1a;                   /*      4521 */

   PEXP - sca;                   /*           */
   PEXP - ssa;                   /*           */
   PEXP - sia;                   /*           */
   PEXP - sla;                   /*           */
   PEXP - sxa;                   /*           */

   PEXP - uca;                   /*           */
   PEXP - usa;                   /*           */
   PEXP - uia;                   /*           */
   PEXP - ula;                   /*           */
   PEXP - uxa;                   /*           */

   PEXP - fta;                   /* 1813      */
   PEXP - dba;                   /* 1813      */
   PEXP - lda;                   /* 1813      */



   bla - PEXP;                   /*      4501 */

   'a' - PEXP;                   /*           */

   n1a - PEXP;                   /*      4521 */

   sca - PEXP;                   /* 1811      */
   ssa - PEXP;                   /* 1811      */
   sia - PEXP;                   /* 1811      */
   sla - PEXP;                   /* 1811      */
   sxa - PEXP;                   /* 1811      */

   uca - PEXP;                   /* 1812      */
   usa - PEXP;                   /* 1812      */
   uia - PEXP;                   /* 1812      */
   ula - PEXP;                   /* 1812      */
   uxa - PEXP;                   /* 1812      */

   fta - PEXP;                   /* 1813      */
   dba - PEXP;                   /* 1813      */
   lda - PEXP;                   /* 1813      */

   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.



(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