/*
target: mcs51
command: sdcc --xram-loc 0x3000 bug0.c
version: 4.0.0
*/
#include <stdio.h>
int f(unsigned char u) {
return u&4 ? u : -1; // returns negative int
//return u&4 ? (int)u : -1; // works
}
void main(void) {
printf("%d\n", f('\x9f'));
for(;;) {}
}
I think this should print 159 (hex 9f) but it prints -97.
As shown in the commented out line, explicitly casting 'u' to int makes it work.
I'm no language lawyer but I think it should automatically convert 'u' to int because the other side (-1) is an int.
sdcc 4.0.0 was released three years ago. Even since the most recent release, 4.2.0, a lot of type-related bugs have been fixed. Did you check if this bug still exists with a current snapshot or sdcc from trunk in svn?
sorry didn't realize that was so old. however behavior is the same in this snapshot:
4.2.14 #14052 (Linux)
Thanks. I can reproduce the issue on my Debian GNU/Linux testing system on amd64 (I checked for stm8, but since type bugs tend to be front-end issues they typically affect all ports the same).
Fixed in [r14055].
Related
Commit: [r14055]