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 unsignedchar
,short
, orint
. In this case, the operation returns achar
.When the first operand of the subtraction (-) operation is a
char
and the second is a signed or unsignedchar
,short
, orint
. If both operands arechar
, the operation returns a standard type. Otherwise, the operation returns achar
.
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
Check Information
Group: The Essential Type Model |
Category: Required |
AGC Category: Advisory |