Hi All, I don't believe this is actually a bug, but can anybody explain why this code works correctly
void func(int v1, int v2){ unsigned char code tbl[] = {'a', 'b', 'c', 'd', 0, '1', '2', '3', '4'}; signed char xdata diff = v1 - v2; if (diff >= -4 && diff <= 4) printf("%c\n", tbl[diff + 4]); }
void func(int v1, int v2){ unsigned char code tbl[] = {'a', 'b', 'c', 'd', 0, '1', '2', '3', '4'}; int xdata diff = v1 - v2; if (diff >= -4 && diff <= 4) printf("%c\n", tbl[diff + 4]); }
I know what a "character" is, but I've always wondered what a negative signed char would be. Just a negative number in a small datatype (8-byte in C51, may be larger elsewhere). It's usually a good idea to think of all actual characters (i.e. the things written in 'a' notation) as unsgined values. That's what the C library does, too: see the description of <ctype.h> as an example.