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

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

    The problem is that you invoked Undefined Behaviour.


    When you invoke UB anything can happen.

    The assignments are ok; there is an implicit conversion in the first line

    int x = 0xFFFFFFFF;
    unsigned int y = 0xFFFFFFFF;
    

    However, the call to printf, is not ok

    printf("%d, %d, %u, %u", x, y, x, y);
    

    It is UB to mismatch the % specifier and the type of the argument.
    In your case you specify 2 ints and 2 unsigned ints in this order by provide 1 int, 1 unsigned int, 1 int, and 1 unsigned int.


    Don't do UB!

提交回复
热议问题