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). It 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
langc
char c = 200;
int i = 1000;
printf("i/c = %d\n", i/c);

...

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.

Code Block
bgColor#ccccff
langc
unsigned char c = 200;
int i = 1000;
printf("i/c = %d\n", i/c);

...