This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

bug in the compiler??

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]);

}
and this doesn't?
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]);

}
Is this behaviour ANSI-compliant?

Cheers

Parents
  • "You are comparing signed char
    with int (signed or unsigned vill vary from compiler to compiler).
    Thus a compiler that default to signed will work with the above, whereas a compiler that default to unsigned will not."

    I'm afraid you're way off the mark here. 'int' is always signed, this is a requirement of the language. 'char' may be signed or unsigned according to the implementation.

    I haven't got time to look into the OP's problem in detail just now, but I'm tending towards a compiler bug.

Reply
  • "You are comparing signed char
    with int (signed or unsigned vill vary from compiler to compiler).
    Thus a compiler that default to signed will work with the above, whereas a compiler that default to unsigned will not."

    I'm afraid you're way off the mark here. 'int' is always signed, this is a requirement of the language. 'char' may be signed or unsigned according to the implementation.

    I haven't got time to look into the OP's problem in detail just now, but I'm tending towards a compiler bug.

Children