Write a “C” function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in “result” and returns 0. Otherwise it returns -1. The solution of casting to long and adding to find detecting the overflow is not allowed.
Read full article from Check for Integer Overflow | GeeksforGeeks
int
addOvf(
int
* result,
int
a,
int
b)
{
*result = a + b;
if
(a > 0 && b > 0 && *result < 0)
return
-1;
if
(a < 0 && b < 0 && *result > 0)
return
-1;
return
0;
}
int
addOvf(
int
* result,
int
a,
int
b)
{
if
( a > INT_MAX - b)
return
-1;
else
{
*result = a + b;
return
0;
}
}