Rule Definition
The use of increment and decrement operators in combination with other operators is not
recommended because:
• It can significantly impair the readability of the code;
• It introduces additional side effects into a statement with the potential for undefined behavior
It is safer to use these operators in isolation from any other arithmetic operators
Remediation
Use increment decrement operators in isolation from the other side-effects
Violation Code Sample
The expression:
u8a = u8b++
is non-compliant. The non-compliant expression statement:
u8a = ++u8b + u8c--;
____________________
The following are all non-compliant because they contain a function call as well as an increment or
decrement operator:
if ( ( f ( ) + --u8a ) == 0u )
{
}
g ( u8b++ );
The following are all non-compliant even though the sub-expression containing the increment or
decrement operator or some other side eff ect is not evaluated:
u8a = ( 1u == 1u ) ? 0u : u8b++;
if ( u8a++ == ( ( 1u == 1u ) ? 0u : f ( ) ) )
{
}
___________________________________________________________________________________________________
Fixed Code Sample
++u8b;
u8a = u8b + u8c;
u8c--;
______________
The following are all compliant because the only side eff ect in each expression is caused by the
increment or decrement operator.
x++;
a[ i ]++;
b.x++;
c->x++;
++( *p );
*p++;
( *p )++;
Reference
Standards Reference:
MISRA C 2012, 13.1: A full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operator
MISRA C++ 2008, 5-2-10: The increment (++) and decrement (--) operators should not be mixed with other operators in an expression
Related Technologies
Technical Criterion
Complexity - Algorithmic and Control Structure Complexity
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.