0

Is there a way in C or C++ to check whether the number provided by the user is outside the range of an integer type?

For instance, let us assume that we are using 16-bit signed integers with a range of -32768 to 32767.

If the user enters 55555 into the program, this will be wrapped to become a negative number so that if you are using a function which can accept any number, the result would be wrong.

Is there a way in C or C++ to determine whether the number provided by the user is within the range of the integer type?


Update: I am using the numbers in a simple subtraction program which accepts two numbers and subtracts the second from the first.

3
  • 1
    What programming language do you use? C++ and C aren't the same, particularly in the area of reading input. Commented Oct 17, 2012 at 15:48
  • @aam1r - I don't think this is actually a duplicate. That link is regarding detecting integer overflow during a mathematical operation, Matthew's question is regarding integer overflow when getting a value from the user. Commented Oct 17, 2012 at 15:54
  • @Mike: Sorry, I misread the question. Thanks for pointing this out! Commented Oct 17, 2012 at 16:32

4 Answers 4

5

If you're using something like strtol it'll set errno to ERANGE if an overflow occurs

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

1 Comment

Correct, but I believe the concern was over whether one could detect an overflow during the conversion process from text to an integer. Once you have it in an integer, you can then do your own range checking to make sure it's within range of whatever datatype you are casting down to (ie short).
3

If the user enters 55555 into the program, this will be wrapped to become a negative number

Not always. It depends upon how you read the number. If you use, operator>>, for example, that value will not be wrapped, it will be rejected.

Is there a way in C or C++ to determine whether the number provided by the user is within the range of the integer type?

In C++, just use operator>>:

#include <iostream>
int main() {
  signed short int i;
  std::cin >> i;
  if(std::cin)
    std::cout << "You entered a valid number!\n";
  else
    std::cout << "Aw c'mon, play by the rules.\n";
}

Comments

0

Is there a way in C or C++ to check whether the number provided by the user is outside the range of an integer type?

It depends of what function you are using (strtol can do that for example). Check the documentation.

2 Comments

strtol is for long. The function for int is strtoimax.
@Barmar That's for intmax_t, which usually is larger than int.
0

The only difference between int's and uint's is how INTERPRET them. Your problem has no solution, if it so critical for you consider switching to floating point argument, checking value and typecasting. Update: I thought you want run time check function argument value provided by another programmer, not console input.

1 Comment

While true most of the time in practice and especially true in the CPU, the compiler and the language standard beg to differ.

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.