Synopsis: | Mixed signed/unsigned type arithmetic shall not be used. |
Language: | C |
Severity Level: | 2 |
Category: | Conversions |
Description: |
Justification In several cases with mixed signed/unsigned integral operands of binary operators, the signed operand will be converted to an unsigned value. This can lead to unexpected changes of its value, resulting in unexpected results of the expression. Example bool b; int i; char c; short s; unsigned int ui; double d; float f; b = (i <= ui); /* WRONG: mixing signed with unsigned types gives * unexpected results. * If i = -10 and ui = 0 then i <= ui evaluates * to FALSE. */ c = c + i; /* WRONG: Possibly mixing signed and unsigned since * the definition of char being signed/unsigned * is implementation defined. */ d = d * ui; /* RIGHT: mixing signed double with unsigned is an * exception to this rule since it is * perfectly safe. */ i = s % i; /* RIGHT */ d = f / d; /* RIGHT: mixing floats with doubles is allowed. */ |