# Thread: how to handle integer overflow in C

1. ## how to handle integer overflow in C

How to handle integer overflow in C

Like in the following multiplication program if u give the numbers as 50000 100000 the result is 7502634 which is not correct how do we handle it

#include<stdio.h>

int main()
{
int a,b;
long int result;
printf("enter the numbers to multiply\n");
scanf("%d\n %d", &a, &b);
result = a * b;
if(result > 0){
printf("result is %d\n", result);
return 0;}
else{
printf("result not in range\n");
return -1;
}
} 2. It displays 500000000 on my compile. It could be the printf on your compile!
Try %ld
Code:
`printf("result is %ld\n", result);` 3. Like in the following multiplication program if u give the numbers as 50000 100000 the result is 7502634 which is not correct how do we handle it
If we are assuming that a and b are positive, we know the multiplication would be incorrect if a * b > INT_MAX, so would checking to see if b > INT_MAX / a do it? Something like this.
Code:
```#include<stdio.h>
#include<limits.h>

void foo(int a, int b)
{
printf("a = %d, b = %d, ", a, b);
if ( b > INT_MAX / a )
{
puts("overflow warning");
return;
}
printf("a * b = %d\n", a * b);
}

int main(void)
{
int a, b;
for(a = 10, b = 5; b < a && a < INT_MAX / 10; a *= 10, b *= 10)
{
foo(a,b);
}
printf("INT_MAX = %d\n", INT_MAX);
return 0;
}

/* my output
a = 10, b = 5, a * b = 50
a = 100, b = 50, a * b = 5000
a = 1000, b = 500, a * b = 500000
a = 10000, b = 5000, a * b = 50000000
a = 100000, b = 50000, overflow warning
a = 1000000, b = 500000, overflow warning
a = 10000000, b = 5000000, overflow warning
a = 100000000, b = 50000000, overflow warning
INT_MAX = 2147483647
*/``` 4. ## thank

Thanks a lot guys. I appreciate ur help. 5. ## but some problem

But the code which u wrote doesnt work for negative values 6. How to handle integer overflow for both positive and negative numbers can anyone help me pleaseeeeeeeee. 7. You've already got 1/4 of it. Here are some hints for the missing pieces.
• A negative number is less than zero
• positive * positive = positive and positive / positive = positive (the part you've got)
• positive * negative = negative and positive / negative = negative
• negative * positive = negative and negative / positive = negative
• negative * negative = positive and negative / negative = positive
• INT_MAX is the most positive int
• INT_MIN is the most negative int
What code have you tried that didn't work?

Aw, heck. The prodding doesn't seem to be working. The hinting to kate1234 to post some code, any code, that makes a vague attempt at expanding on my previous post seems to have been in vain.

I had written the following code shortly after I posted the first, anticipating a followup question for handling negative numbers (this is why I spelled out this intention by stating, "assuming that a and b are positive").

With the subsequent posts, it seemed that a solution was wanted with no effort. Ten days or so have not seemed to produce any code on that end. Nor even a question as to why things that were tried did not seem to work.

Well, maybe I'm in a good mood, or maybe I just want to justify some of the smart-ass comments above. Or maybe I want to see if anyone can point out things that I've overlooked. But here is what I'd come up with. (I added the comments after this original post.)
Code:
```#include<stdio.h>
#include<limits.h>

int foo(int a, int b, int *result)
{
if(a > 0 && b > 0)
{
/*
* 'a' is positive, 'b' is positive, product will be positive.
* See if one positive value 'b' is more than the most positive int
* divided by the other positive value 'a' (positive quotient).
*/
if ( b > INT_MAX / a )
{
return -1;
}
}
else if (a > 0 && b < 0)
{
/*
* 'a' is positive, 'b' is negative, product will be negative.
* See if the negative value 'b' is less than the most negative int
* divided by the positive value 'a' (negative quotient).
*/
if ( b < INT_MIN / a )
{
return -1;
}
}
else if (a < 0 && b > 0)
{
/*
* 'a' is positive, 'b' is negative, product will be negative.
* See if the negative value 'a' is less than the most negative int
* divided by the positive value 'b' (negative quotient).
*/
if ( a < INT_MIN / b )
{
return -1;
}
}
else /* if (a < 0 && b < 0) */
{
/*
* 'a' is negative, 'b' is negative, product will be positive.
* See if one negative value 'b' is less than the most positive int
* divided by the other negative value 'a' (negative quotient).
*/
if ( b < INT_MAX / a )
{
return -1;
}
}
*result = a * b;
return 0;
}

void bar(int a, int b)
{
int product;
printf("a = %d, b = %d, ", a, b);
if(foo(a, b, &product) < 0)
{
puts("overflow warning");
}
else
{
printf("a * b = %d\n", a * b);
}
}

int main(void)
{
int a, b;
printf("INT_MAX = %d\n", INT_MAX);
printf("INT_MIN = %d\n", INT_MIN);
for(a =  10, b =  5; a < INT_MAX / 10; a *= 10, b *= 10)
{
bar(a,b);
}
for(a =  10, b = -5; a < INT_MAX / 10; a *= 10, b *= 10)
{
bar(a,b);
}
for(a = -10, b =  5; a > INT_MIN / 10; a *= 10, b *= 10)
{
bar(a,b);
}
for(a = -10, b = -5; a > INT_MIN / 10; a *= 10, b *= 10)
{
bar(a,b);
}
return 0;
}

/* my output
INT_MAX = 2147483647
INT_MIN = -2147483648
a = 10, b = 5, a * b = 50
a = 100, b = 50, a * b = 5000
a = 1000, b = 500, a * b = 500000
a = 10000, b = 5000, a * b = 50000000
a = 100000, b = 50000, overflow warning
a = 1000000, b = 500000, overflow warning
a = 10000000, b = 5000000, overflow warning
a = 100000000, b = 50000000, overflow warning
a = 10, b = -5, a * b = -50
a = 100, b = -50, a * b = -5000
a = 1000, b = -500, a * b = -500000
a = 10000, b = -5000, a * b = -50000000
a = 100000, b = -50000, overflow warning
a = 1000000, b = -500000, overflow warning
a = 10000000, b = -5000000, overflow warning
a = 100000000, b = -50000000, overflow warning
a = -10, b = 5, a * b = -50
a = -100, b = 50, a * b = -5000
a = -1000, b = 500, a * b = -500000
a = -10000, b = 5000, a * b = -50000000
a = -100000, b = 50000, overflow warning
a = -1000000, b = 500000, overflow warning
a = -10000000, b = 5000000, overflow warning
a = -100000000, b = 50000000, overflow warning
a = -10, b = -5, a * b = 50
a = -100, b = -50, a * b = 5000
a = -1000, b = -500, a * b = 500000
a = -10000, b = -5000, a * b = 50000000
a = -100000, b = -50000, overflow warning
a = -1000000, b = -500000, overflow warning
a = -10000000, b = -5000000, overflow warning
a = -100000000, b = -50000000, overflow warning
*/```
Posting this as an edit, rather than a new reply, will leave a minimal effort to kate1234. A solution is now here if you bother to look.
[/edit] 8. Type casting. Promote integers to long. 9. Originally posted by Dev
Type casting. Promote integers to long.
But a long isn't guaranteed to be any bigger than an int. Popular pages Recent additions 