Thread: how to handle integer overflow in C

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    10

    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. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    It displays 500000000 on my compile. It could be the printf on your compile!
    Try %ld
    Code:
    printf("result is %ld\n", result);
    Last edited by Scarlet7; 04-11-2003 at 04:11 PM.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    10

    thank

    Thanks a lot guys. I appreciate ur help.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    10

    but some problem

    But the code which u wrote doesnt work for negative values

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    How to handle integer overflow for both positive and negative numbers can anyone help me pleaseeeeeeeee.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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?

    [edit]
    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]
    Last edited by Dave_Sinkula; 04-24-2003 at 06:04 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    Type casting. Promote integers to long.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Dev
    Type casting. Promote integers to long.
    But a long isn't guaranteed to be any bigger than an int.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
  5. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM