0
#include <stdio.h>

int main()
{
    char c = -65;
    unsigned char uc = 191, uc2 = c;
    printf("%d %d", uc == c, uc2);
}

expecting 1, because uc is '10111111' and c is '10111111', actual result is 0

Chen Zhao
  • 1
  • 2
  • Are you sure that `char` is signed on your target and with your compiler? Unlike other integer types, `char` can be either signed or unsigned, but it's up to the compiler to decide. – Some programmer dude Feb 27 '23 at 09:19
  • Basically it gets converted into `int` before the comparison. Only as an non lvalue in that expression (aka the object type is not changed). But yeah you can read like the C standard on this. (3.2 Conversions - fipspub160) – AnArrayOfFunctions Feb 27 '23 at 09:20
  • @MIke I'm laughing - this won't work though. But maybe I'm wrong at some points anywho - in any case its worth reading nonetheless. – AnArrayOfFunctions Feb 27 '23 at 09:25
  • https://godbolt.org/z/djWoTc6G6 <- comparison is `cmp ecx, eax`, the difference is between `movzx ecx, BYTE PTR [rbp-2]` (zx = zero extend) and `movsx eax, BYTE PTR [rbp-1]` (sx = sign extend) – teapot418 Feb 27 '23 at 09:56

0 Answers0