...
In this noncompliant code example, the char
-type variable c
may be signed or unsigned. Assuming 8-bit, two's complement character types, this code may either print out i/c = 5
(unsigned) or i/c = -17
(signed). As a result, it I is much more difficult to reason about the correctness of a program without knowing if these integers are signed or unsigned.
Code Block | ||
---|---|---|
| ||
char c = 200; int i = 1000; printf("i/c = %d\n", i/c); |
...
Compliant Solution
In this compliant solution, the variable c
is declared as unsigned char
. The subsequent division operation is now independent of the signedness of char
and consequently has a predictable result.
...