Here is from C++ Prime 5th:
if we use both unsigned and int values in an arithmetic expression, the int value ordinarily is converted to unsigned.
And I tried the following code:
int i = -40;
unsigned u = 42;
unsigned i2 = i;
std::cout << i2 << std::endl; // 4294967256
std::cout << i + u << std::endl; // 2
For i + u
, if i
is converted to unsigned
, it shall be 4294967256
, and then i + u
cannot equal to 2
.
Is int
value always converted to unsigned
if we use both unsigned
and int
values in an arithmetic expression?