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

后端 未结 9 552
时光取名叫无心
时光取名叫无心 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:45

    Yes, because in your case they use the same representation.

    The bit pattern 0xFFFFFFFF happens to look like -1 when interpreted as a 32b signed integer and as 4294967295 when interpreted as a 32b unsigned integer.

    It's the same as char c = 65. If you interpret it as a signed integer, it's 65. If you interpret it as a character it's a.


    As R and pmg point out, technically it's undefined behavior to pass arguments that don't match the format specifiers. So the program could do anything (from printing random values to crashing, to printing the "right" thing, etc).

    The standard points it out in 7.19.6.1-9

    If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

提交回复
热议问题