Main Content

MISRA C:2012 Rule 10.2

Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations

Description

Note

Using Code Prover for checking coding rules is no longer supported. See Version History.

Rule Definition

Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations.

Rationale

Essentially character type expressions are char variables. Do not use char in arithmetic operations because the data does not represent numeric values.

It is appropriate to use char with addition and subtraction operations only in the following cases:

  • When one operand of the addition (+) operation is a char and the other is a signed or unsigned char, short, or int. In this case, the operation returns a char.

  • When the first operand of the subtraction (-) operation is a char and the second is a signed or unsigned char, short, or int. If both operands are char, the operation returns a standard type. Otherwise, the operation returns a char.

The above uses allow manipulation of character data such as conversion between lowercase and uppercase characters or conversion between digits and their ordinal values.

For more information on essential types, see Essential Types in MISRA C Rules 10.x.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include<stdint.h>
typedef double float64_t;
extern uint8_t u8a;
extern int8_t s8a;
extern int16_t s16a;
extern int32_t s32a;
extern float64_t fla;
extern long lval;

void foo ( void )
{
	char cha;

	s16a = s16a - 'a';  /* Noncompliant*/
	
	cha = '0' + fla;    /* Noncompliant*/
	
	cha = cha  + ':';   /* Noncompliant*/
	
	cha += lval;  /* Noncompliant*/
}

  • You cannot subtract a char-type variable from an integer. When you subtract 'a' from the integer s16a, Polyspace® reports a violation.

  • In addition operations, char type variables can only be added to integer type variables. When you add the floating point number fla to '0', Polyspace reports a violation.

  • You cannot add a char-type variable with a long type variable. When you add lval to cha, Polyspace reports a violation.

  • The arithmetic operation cha+':' is not a conversion from upper to lowercase or from digit to cardinal value. Polyspace reports a violation when char variables are used in arithmetic expressions.

#include<stdint.h>
typedef double float64_t;
extern uint8_t u8a;
extern int8_t s8a;
extern int16_t s16a;
extern int32_t s32a;
void foo ( void )
{
	char cha;

	cha = '0' + u8a;     /* Compliant*/
	
	cha = s8a + '0';     /* Compliant*/
	
	s32a = cha  - '0';   /* Compliant*/
	
	cha = '0' - s8a;     /* Compliant*/
	
	cha++;               /* Compliant*/
}

char type variables can be used in certain addition or subtraction operations to perform char data manipulations. For instance:

  • You can add an unsigned integer u8a to the char type data '0' to convert from '0' to a different character.

  • Similarly, you can add the signed integer s8a to '0' to perform a desired character conversion.

  • You can also subtract s8a from the char data '0'.

  • Incrementing and decrementing char data is also permissible.

Check Information

Group: The Essential Type Model
Category: Required
AGC Category: Advisory

Version History

expand all