[previous] MISRA-C:2004  Rule  12.9:  (Required) [next] The unary minus operator shall not be applied to an expression whose underlying type is unsigned.
Applying the unary minus operator to an expression of type unsigned int or unsigned long generates a result of type unsigned int or unsigned long respectively and is not a meaningful operation. Applying unary minus to an operand of smaller unsigned integer type may generate a meaningful signed result due to integral promotion, but this is not good practice.

See section 6.10 for a description of underlying type.

Example Code:


/* PRQA S 3199,3203 ++ */

#include "misra.h"
#include "m2cmex.h"

extern S16 test_1209( void )
{

   u32r = -u32a;             /* MISRA Violation */
   s16a = -u8a;              /* MISRA Violation */

   return 1;
}


QAC messages that encompass this guideline:

3101 Unary '-' applied to an operand of type unsigned int or unsigned long gives an unsigned result.
3102 Unary '-' applied to an operand whose underlying type is unsigned.



(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