0
int main(){
    char *mem = malloc(sizeof(char)*10);
    for(size_t i = 0; i < 10; i++){
        mem[i] = 0xFF;
    }

    for (size_t i = 0; i < 10; i++){
        if(mem[i] == 0xFF){
            printf("True\n");
        }
    }
}

This doesn't print out True. However, when mem is declared as unsigned char, True is printed. How does comparision take place? I thought that == compares bit values, so why does it matter if it is char/unsigned char?

Kartik
  • 41
  • 2
  • I can't seem to find a good duplicate written for C so I've linked a couple of C++ questions that have relevant answers. – John Kugelman Sep 03 '20 at 04:25
  • "compares bit values" --> yes.When `char` is _signed_ with a value of -1 promoted to `int`: 0b11111111 11111111 11111111 11111111 != 0b00000000 00000000 00000000 11111111. -1 != 255. – chux - Reinstate Monica Sep 03 '20 at 04:29
  • @JohnKugelman The [C wiki](https://stackoverflow.com/tags/c/info) got a bunch of useful canonical dupes below FAQ, including [Is char signed or unsigned by default?](https://stackoverflow.com/questions/2054939/is-char-signed-or-unsigned-by-default). I added it to the duplicate list. – Lundin Sep 03 '20 at 10:02

0 Answers0