[previous] 3101 [next] Unary '-' applied to an operand of type unsigned int or unsigned long gives an unsigned result.
Arithmetic type - Operands REFERENCE - ISO:C90-6.3.3.3 Unary Arithmetic Operators - Semantics

A unary minus operator (-) is being applied to an expression which is unsigned and the result is likely to be meaningless. Applying a unary '-' to an expression of type unsigned int, unsigned long or unsigned long long will always generate a result which is of the same type as the operand - i.e. an unsigned type. The result is computed by subtracting the value of the unsigned operand from the number which is one greater than the maximum unsigned value representable in that type.

Note, however, that when a unary '-' operator is applied to an unsigned operand of small integer type, the result may well be a negative value of type signed int because integral promotion will be applied to the operand.

For example


/*PRQA S 2017,3199,3203,3227,3408,3447 ++*/
/*****************************************/
/* OPTIONS: -s int=16 -ss-               */
/*****************************************/

void foo(unsigned int i)
{
    long j;

    j = -i;                     /* Message 3101 */
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents