If I have:
signed char * p;
and I do a comparison:
if ( *p == 0xFF ) break;
it will never catch 0XFF, but if I replace it with -1 it will:
if ( *p == (signed char)0xFF ) break;
How can this happen? Is it something with the sign flag? I though that 0xFF == -1 == 255.
The value
0xFFis a signed int value. C will promote the*pto anintwhen doing the comparison, so the first if statement is equivalent to:which is of course false. By using
(signed char)0xFFthe statement is equivalent to:which works as you expect. The key point here is that the comparison is done with
inttypes instead ofsigned chartypes.