4

I want to know how to determine overflow in C/C++. if my input for integer is 9999999999999999999999 , It is a very big number and if I run the below code I will get a garbage output.

#include <stdio.h>

int main(){
    int a;
    scanf("%d",&a);
    printf("%d",a);
    return 0;
}

Is there any way to know , if the input is a big number, I can output "Input is too big" .

Note that, I have already checked How to detect integer overflow? . But the question is different from my one.

4

5 回答 5

4

In your case, read the input in a string and then, depending of the length, make a decision. You can encode in string the limits of integer, long long etc and if the input has the length (the number of figures) equal or less than one of your string limits, move one with the comparison and if it is smaller than the string representation of a limit, you can safely convert it to an integer type.

UPDATE

or if you prefer you can use stream operators in C++ as David Brown suggested...

于 2013-10-27T18:31:36.060 回答
3

As posted in comments there is a way to detect overflow after arithmetic operation, which is partially helpful in this case:

What you can do is to read char by char and check for overflow at every step: (Not sure if you use C or C++, either include <limits> or <limits.h>)

int x=0;
while(1){
 char c='\0';
 scanf(" %c",&c);
 if (c<'0' || c>'9')
  break;//Input finished
 if (!willOverflow(x,c-'0'))
  x=x*10+c-'0';
 else
  break;//Overflow would happen, handle code belongs here
}

int willOverflow(int cur,int next){//Perform your overflow check
//for example i would use
 return ((INT_MAX-next)/10)<cur);
}
于 2013-10-27T19:13:12.613 回答
2

This catches other errors as well as overflow:

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>

int main() {
    std::string line;
    std::getline(std::cin, line);
    try {
        std::cout << boost::lexical_cast<int>(line) << "\n";
    } catch (const boost::bad_lexical_cast &e) {
        std::cout << e.what() << "\n";
    }
}

Boost of course is non-standard, you'll have to install it for your system. Failing that, or if you want the library function to distinguish input "99999999999" from "123abc" for you then you'll have to use std::stoi or std::strtol. Both are somewhat less convenient.

于 2013-10-27T19:21:21.173 回答
1

Yes, you can check for overflow of numbers read from input, but scanf is not the way to do it. Calling scanf("%d", &n) when the input number is too big to be represented as an int actually has undefined behavior. (IMHO this is very unfortunate, and makes scanf nearly impossible to use safely for numeric input.)

But the strto* functions:

  • strtol (for long)
  • strtoll (for long long)
  • strtoul (for unsigned long)
  • strtoull (for unsigned long long)
  • strtof (for float)
  • strtod (for double)
  • strtold (for long double)

though they're a bit more difficult to use, have well defined behavior for all inputs.

Use fgets to read a line of input, then use one of the strto* functions to convert the input to a number of the appropriate type.

On overflow, these functions return the minimum or maximum value of the appropriate type and set errno to ERANGE. You should set errno to 0 before the call. (Checking errno setting lets you distinguish between an overflow and an actual input of, say, 2147483647.)

Read the man documentation for the appropriate function for the gory details.

Since there's no strtoi function for int, you can use strtol, check whether the input was a valid long, and then check whether the long value is in the range INT_MIN .. INT_MAX; similarly for unsigned int and strtoul.

于 2013-11-02T03:24:19.517 回答
0

Reading in a string and then checking the string is the way to go, if you need to check for such a thing.

于 2013-10-27T18:45:52.383 回答