Why is an unsigned int 1 lower than a char y -1?

前端 未结 2 1268
北荒
北荒 2020-12-20 16:19
main() {
    unsigned x = 1;
    char y = -1;

    if (x > y)
        printf(\"x>y\");
    else
        printf(\"x<=y\");
}

I expected

2条回答
  •  生来不讨喜
    2020-12-20 16:22

    When using signed and unsigned in single operation the signed got promoted to unsigned by C's automatic type conversion. If the bit patter of -1 is considered an unsigned number then it is a very very high value. So x > y is false.

提交回复
热议问题