int res = -2147483648 / -1;
cout << res << endl; // prints -2147483648
cout << -2147483648 / -1 << endl; // prints 2147483648
int res = numerator / denominator; // produces SIGFPE, Arithmetic exception interrupt
Note there're no negative integer literals.
There are no negative integer literals. Expressions such as -1 apply the unary minus operator to the value represented by the literal, which may involve implicit type conversions.
The literal 2147483648
is larger than the max value of int
, so its type will be long
(or long long
, depends on implementation). Then -2147483648
's type is long
, and the result of calculation (-2147483648 / -1
) is long
too.
For the 1st case, the result 2147483648
of type long
is implicitly converted to int
, but it's larger than the max value of int
, the result is implementation-defined. (It seems the result is wrapped around according to the rules of the representation (2's complement) here, so you get the result -2147483648
.)
For the 2nd case, the result with type long
is printed out directly, so you get the correct result.
For the 3rd case, you're doing the calculation on two int
s, and the result can't fit in the result type (i.e. int
), signed integer arithmetic operation overflow happened, the behavior is undefined. (Produces SIGFPE, Arithmetic exception interrupt here.)