The first case doing scaling for int explicit:
cout << ((int)c1 * c2 / c1 << endl;
second case, assigning an intermediate variable is equivalent
cout << ((signed char)((int)c1 * c2)) / c1 << endl;
(The implicit int casts made by the compiler are made explicit.)
With two additions, multiplying the largest negative type value by -1, the value remains unchanged if it is limited to the same number of bits. So, the second example: c1 * c2 == c1. Although the multiplication is done as an int, it returns to the width of the char.
In the first example, the entire calorie is executed as an int, so the operation has more bits to work, and therefore truncation does not occur. Here c1 * c2 == (c1 * -1) == -c1. Hence another result.
source share