Forum Discussion
Altera_Forum
Honored Contributor
20 years agochar takes 4 bytes?
char test_char=-5;
printf("test_char %x,%x,%x\n",test_char,(char)test_char,(unsign char)test_char); We expect the output was: >test_char fb,fb,fb but we got : >test_char fffffffb,ffff...
Altera_Forum
Honored Contributor
20 years agoHi All,
> what should we do? Start by writing good code ... pick your data type and stick with it. The result of relational and equality operators is of type int when the operands are integral types ... so, char and unsigned char are promoted to type int (the range of both can be represented by an int) ... and the sign is preserved (or "extended" into the higher-order bits). Basically, if (for whatever reason) bit 7 of your 8-bit data is not a sign bit, then don't treat it as such -- use unsigned char instead (and ditto for short and unsigned short) ... you'll save yourself some headaches and you can use:if(test_char==good) with confidence. Regards, --Scott