12

How can I get an equivalent of java.lang.Integer.MIN_VALUE on C++?

dmessf
  • 1,025
  • 3
  • 11
  • 19

2 Answers2

20
#include <limits>    
std::numeric_limits<int>::min();
CTT
  • 16,901
  • 6
  • 41
  • 37
3

Depends what you mean by "equivalent". java.lang.Integer.MIN_VALUE is a compile-time constant in Java, but std::numeric_limits<int>::min() is not an integer constant expression in C++. So it can't be used for example as an array size (well, the min value of an int can't anyway because it's negative, but the same goes for expressions involving it, or other similar values, or other contexts requiring an i.c.e).

If you need a compile-time constant in C++, use INT_MIN from <climits>. In fact you may as well use it anyway: numeric_limits is essential if you're writing generic code, and you have some integer type T which might be int, or might be something else. Its primary use otherwise is to prove your leet C++ skillz, and/or make your code longer ;-)

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699