...
Code Block | ||
---|---|---|
| ||
char c = 200; int i = 1000; printf("i/c = %d\n", i/c); |
Assuming 8-bit, two's complement character types, this code may print out either i/c = 5
(unsigned) or i/c = -17
(signed). As a result, it is much more difficult to reason about the correctness of a program without knowing if these integers are signed or unsigned.
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.
...