Handling Overflow
#include <iostream>
using namespace std;
#include <climits>
int summ(int a,int b){
int sum=a+b;
if (a>=0 and b>=0 and sum>=0)
return sum;
if (a<=0 and b<=0 and sum<=0)
return sum;
if (a>0 and b<0)
return sum;
if (a<0 and b>0)
return sum;
else{
printf("overflowed...");
return -1;
}
}
int main(){
int a = summ(INT_MAX-1000,INT_MIN);
cout<<a;
}
Comments
Post a Comment