The real difference between “int” and “unsigned int”

后端 未结 9 548
时光取名叫无心
时光取名叫无心 2020-12-12 16:13

int:

The 32-bit int data type can hold integer values in the range of −2,147,483,648 to 2,147,483,647. You may also refer to this data type as sig

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 16:55

    The internal representation of int and unsigned int is the same.

    Therefore, when you pass the same format string to printf it will be printed as the same.

    However, there are differences when you compare them. Consider:

    int x = 0x7FFFFFFF;
    int y = 0xFFFFFFFF;
    x < y // false
    x > y // true
    (unsigned int) x < (unsigned int y) // true
    (unsigned int) x > (unsigned int y) // false
    

    This can be also a caveat, because when comparing signed and unsigned integer one of them will be implicitly casted to match the types.

提交回复
热议问题