Why compiler is not giving error when signed value is assigned to unsigned integer? - C++

后端 未结 5 1463
说谎
说谎 2020-12-16 12:09

I know unsigned int can\'t hold negative values. But the following code compiles without any errors/warnings.

unsigned int a = -10;
<
5条回答
  •  旧时难觅i
    2020-12-16 13:10

    -10 is parsed as an integer value, and assigning int to unsigned int is allowed. To know you are doing something wrong the compiler has to check whether your integer (-10) is negative or positive. As it is more than a type check, I guess it has been disabled for performance issues.

提交回复
热议问题