When I execute the following code:
cout<<min(INT_MAX, INT_MAX+ INT_MAX);
I obtain -2
as the output. Can someone explain why?
Shouldn't the output be the value of INT_MAX
?
When I execute the following code:
cout<<min(INT_MAX, INT_MAX+ INT_MAX);
I obtain -2
as the output. Can someone explain why?
Shouldn't the output be the value of INT_MAX
?
INT_MAX+ INT_MAX
invokes undefined behavior because signed integer overflow.
C++ Standard :
If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.