Hi,
Since your variables have different types and you didn’t explicitly use the cast operator, before a comparison of two variables, the compiler promotes them (provided the types are compatible) to the biggest type of the two, larger or equal to an
int (I’m talking of sizes in bytes, of course).
Now since in your first case both types are
int-sized, there's no promotion and the result of the comparison is true.
In the second case however, a
unsigned char is promoted to
int with all upper bytes 0, while the
signed char promotes to
int by sign extension, meaning all upper bytes are 1 (in 2’s complement notation).
For example, assuming
sizeof(int) = 2,
cu is promoted to
0x00FF (or the
int 255), while
cs is promoted to
0xFFFF (or the
int -1). These are obviously different…
If you used the cast operator like:
Code:
if (cs == (signed char)cu)
or
Code:
if ((unsigned char)cs == cu)
the comparisons’ results were true in both cases.
Arthur