![]() |
![]() |
3102 | ![]() |
||||
![]() | |||||||
| 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 intrinsically unsigned and the result of such an operation can often 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. In this case, a unary '-' has been applied to an operand whose underlying type is unsigned and is a small integer type. The type of the result will be the type which results from integral promotion of the operand, either signed int or unsigned int. If it happens to be signed int, the result will be the negative equivalent of the original value of the expression. This may be the intended behaviour but it is recommended that a unary '-' operation should only be applied to an operand whose underlying type is signed. For example
/*PRQA S 2017,2100,2211,3198,3199,3203,3408,3447 ++*/
extern unsigned char uca;
extern unsigned char ucb;
extern unsigned int uia;
extern void foo(void)
{
int sia;
unsigned int uib;
uib = -uia; /* Message 3101 */
sia = -uca; /* Message 3102 */
sia = -(uca + ucb); /* Message 3102 */
}
See also:
![]() | ||
| QA·C Source Code Analyser 8.1.2
© 2013 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | Contents |