In this piece of code:
signed char v = -64;
int n = 4;
int x = v - '0' * (signed char)n;
std::cout << x << std::endl;
Should x be -5 or -261? In my understanding, the initializer expression has signed char type, and the type conversion should happen later, once the initializer has been calculated.
So, v - '0' * (signed char)n should be equal to -5 because that's the equivalent value of -261 in signed char valuation.
However, that piece of code prints -261.
chars andshorts are promoted tointwhen doing arithmetic. The(signed char)ncast doesn't help since the result is immediately promoted right back up tointfor the multiplication.Here are the implicit conversions made explicit: