0

I thought that unsigned int max possible value is 65535 ( in C++ ) but I created a programm which can use an int which is equal with 100000 for example. Is it safe to use int with values up to 10000000 or the program may crash? In that case is long the only solution?

Thank you for your time!

3 Answers3

3

Use std::numeric_limits<unsigned int>::max() to know for certain what this value is.

David Sykes
  • 48,469
  • 17
  • 71
  • 80
Mike Woolf
  • 1,210
  • 7
  • 11
  • Is that value only for my PC? – George Vidalakis Jan 11 '14 at 18:32
  • 1
    @GeorgeVidalakis limits.h and numeric_limits are specific for your compiler implementation, its not really "your PC". For example if you compile on a 32 bit implementation then bring that compiled program later to a 64bit machine, you don't magically get a bigger value on the 64bit machine. – Brandin Jan 11 '14 at 18:57
1

INT_MAX is implementation defined. That means it's up to your compiler vendor to decide, as long as it's no less than 32767, and greater or equal to a short. You can use the climits definitions to discover your implementation's limits:

#include <iostream>
#include <climits>

int main () {
  std::cout << INT_MAX << std::endl;
  return 0;
}

On my installation of gcc/g++ v4.8.1 targeting x86_64-linux-gnu, this snippet produces:

2147483647

And as has been mentioned in the followup replies to this answer, you may (and probably should) use the more semantically proper (for C++) method:

#include <iostream>
#include <limits>

int main () {
  std::cout << std::numeric_limits<int>::max() << std::endl;
  return 0;
}

...which ought to produce the same output.

DavidO
  • 13,812
  • 3
  • 38
  • 66
1

Please check the code below for limits:

#include <iostream>     // std::cout
#include <limits>       // std::numeric_limits

int main () {
  std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
  std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
  std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';
  std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';
  std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';
  return 0;
}
vershov
  • 928
  • 4
  • 6