7

Why is there a runtime error? I made range2 a long long.

Code:

/*VARIABLES FOR WHILE LOOP*/
 long long range1 = 9;
 int length = 1;

 /*FIND NUM'S LENGTH*/
 while (NUM > range1)     
 {
    long long range2 = range1 * 10 + 9;
    length += 1;
 }

Error:

credit.c:25:25: runtime error: signed integer overflow: 999999999 * 10 cannot be represented in type 'int'

5
  • length += 1 - how large can that get? Commented Dec 6, 2016 at 3:13
  • I want the limit to be the length of NUM (a credit card number entered by the user). Commented Dec 6, 2016 at 3:17
  • What compiler are you using? 999999999 * 10 is too large to be stored in a signed int, but since range1 is a signed long long, it should suffice. Commented Dec 6, 2016 at 4:13
  • Try changing the 10 to 10LL. Commented Dec 6, 2016 at 4:14
  • @moswald: I'm using cs50's IDE. I used 'LL', but it still gave the error. Commented Dec 6, 2016 at 17:12

4 Answers 4

4

You can try this one to handle the above conditions

With the two most common representations, the range is 0 through 4,294,967,295 (2^32 − 1) for representation as an (unsigned) binary number, and −2,147,483,648 (−2^31) through 2,147,483,647 (2^31 − 1) for representation as two's complement

I'm recently faced it while solving a question having constraints

Given a 32-bit signed integer, reverse digits of an integer

runtime error: signed integer overflow: 999999999 * 10 cannot be represented in type 'int'

So, here is my code

    while (x != 0) {
        int pop = x % 10;
        x /= 10;
        if (rev > INT_MAX/10 || (rev == INT_MAX / 10 && pop > 7)) return 0;
        if (rev < INT_MIN/10 || (rev == INT_MIN / 10 && pop < -8)) return 0;
        rev = rev * 10 + pop;
    }

before I'm getting 999999999*10, rev>INT_MAX it guaranteed OVERFLOW & vice-versa.

Sign up to request clarification or add additional context in comments.

Comments

2

There are (two) major problems with your code, which both lead to the same error:
The int length variable is being increased to a point where it reaches the max values.

Here is you while loop:

while (NUM > range1)
{
    long long range2 = range1 * 10 + 9;
    length += 1;
}

Problem #1

This biggest issue is that this while loop will never end. You never change the values of NUM or range1 in the while loop, so if NUM starts out as greater than range1, you will get stuck in an infinite loop. length += 1 will keep getting called until the length integer reaches the max allowed int value.

(Possible) Problem #2

Depending on how you fix Problem #1, you can also face the following issue.

As you stated in a comment above, NUM is a credit card number.

  • Credit card number by default are 16 digits long.
  • The max value of and int variable is 999999999 * 10 which is max 10 digits.

If your loop is set to run the value of NUM times until it reaches 9. If we let's say pick the lowest possible 16 digit credit card number, 1000000000000000, your loop still will still run 1000000000000000 - 9 times.

Every time your loop runs, length is increased by 1. The loop will basically try to increase the int variable at least 999999999999991 times which will cause the value to become greater than 999999999 * 10.

(Possible) Fix

There are not enough details in your question to know if for sure this is what will solve your problem, but I will guess that instead of

long long range2 = range1 * 10 + 9;

you probably meant to write

range1 = range1 * 10 + 9;

3 Comments

length gets increased only to the NUM's length (digits). My loop is supposed to find the NUM's number of digits.
@KhalidMukadam That is not what your loop currently does. To be clear, if let's say your NUM is 16 digits long, should length end up as 16 or should it be 16 - range1 and only be 5?
@KhalidMukadam Problem #1 explains why you are getting a runtime error and in essense answers your question. It seems though that there are more problems with your code than you indicated in your question. If you are looking for the proper way to find the length of a string, look at: stackoverflow.com/questions/2070566/…
0

You have to carefully read the question because in some question input/output is signed 32-bit integer or unsigned 32-bit integer both have a different range so value outside the range is invalid or you have to return 0 just like bellow e.g. Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Solution

Comments

-1

Size of unsigned int is (0 to 4,294,967,295) for 4 bytes;
So 999999999 *10 is greater than 4,294,967,295 therefore try using (long)

long long int x=999999999 *10   (try this...) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.