Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
bgColor#FFcccc
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.

...