dmessf
dmessf

Reputation: 1013

C++ equivalent of java.lang.Integer.MIN_VALUE

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

Upvotes: 12

Views: 4783

Answers (2)

Steve Jessop
Steve Jessop

Reputation: 279315

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 ;-)

Upvotes: 3

CTT
CTT

Reputation: 17641

#include <limits>    
std::numeric_limits<int>::min();

Upvotes: 20

Related Questions