217

Is there any code to find the maximum value of integer (accordingly to the compiler) in C/C++ like Integer.MaxValue function in java?

GergelyPolonkai
  • 6,230
  • 6
  • 35
  • 69
d3vdpro
  • 2,887
  • 4
  • 25
  • 29

7 Answers7

385

In C++:

#include <limits>

then use

int imin = std::numeric_limits<int>::min(); // minimum value
int imax = std::numeric_limits<int>::max();

std::numeric_limits is a template type which can be instantiated with other types:

float fmin = std::numeric_limits<float>::min(); // minimum positive value
float fmax = std::numeric_limits<float>::max();

In C:

#include <limits.h>

then use

int imin = INT_MIN; // minimum value
int imax = INT_MAX;

or

#include <float.h>

float fmin = FLT_MIN;  // minimum positive value
double dmin = DBL_MIN; // minimum positive value

float fmax = FLT_MAX;
double dmax = DBL_MAX;
Oliver Hanappi
  • 12,046
  • 7
  • 51
  • 68
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • 19
    Note that the floating-point `min` are the minimum *positive* value, where as the integer `min` are the minimum value. Same goes for the C macros/constants. – dalle Dec 31 '09 at 11:29
  • 8
    in C99 you can also use UINT64_MAX and INT64_MAX – Dmitry Vyal Oct 02 '13 at 09:08
  • 5
    @DmitryVyal: Yes you can, but those are the limits of `uint64_t` and `int64_t`, not of `int`. – Keith Thompson Sep 27 '14 at 21:45
  • In C++, I used the same code shown above: `#include ` and `int imax = std::numeric_limits::max();`, but I get the error `Can't resolve struct member 'max'`. Any ideas as to why this occurs, and how to fix it? I am using CLion IDE, with CMake and C++ 11 on Ubuntu 14.04. I think it is linked to [this issue](https://youtrack.jetbrains.com/issue/CPP-222#tab=TeamCity%20Changes) – modulitos Oct 10 '14 at 20:15
  • 1
    Hopefully this helps someone, because it was a CLion IDE bug that I fixed by using the latest CLion (build 138.2344 - CLion is in the Early Access Program phase, and thus unstable) – modulitos Oct 10 '14 at 20:34
  • 2
    a fancy way would be like so `(unsigned)-1/2` – Stavros Avramidis Feb 21 '19 at 20:35
  • I want to use is on an Arduino but it does not exist. "No such file or directory" – Niko O Aug 14 '20 at 15:16
36

I know it's an old question but maybe someone can use this solution:

int size = 0; // Fill all bits with zero (0)
size = ~size; // Negate all bits, thus all bits are set to one (1)

So far we have -1 as result 'till size is a signed int.

size = (unsigned int)size >> 1; // Shift the bits of size one position to the right.

As Standard says, bits that are shifted in are 1 if variable is signed and negative and 0 if variable would be unsigned or signed and positive.

As size is signed and negative we would shift in sign bit which is 1, which is not helping much, so we cast to unsigned int, forcing to shift in 0 instead, setting the sign bit to 0 while letting all other bits remain 1.

cout << size << endl; // Prints out size which is now set to maximum positive value.

We could also use a mask and xor but then we had to know the exact bitsize of the variable. With shifting in bits front, we don't have to know at any time how many bits the int has on machine or compiler nor need we include extra libraries.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
MJeB
  • 369
  • 3
  • 3
  • 1
    `cout << "INT_MAX:\t" << (int) ((~((unsigned int) 0)) >> 1) << '\n' << "UINT_MAX:\t" << ~((unsigned int) 0) << endl;` – Slaiyer Mar 18 '17 at 23:13
17
#include <climits>
#include <iostream>
using namespace std;

int main() {
  cout << INT_MAX << endl;
}
  • 2
    I wouldn't call INT_MAX "a solution for C". It's old-school and deprecated in C++, though. – Paul Tomblin Dec 06 '09 at 14:08
  • 6
    I think both are C++ answers. `numeric_limits::max()` - works also in template contexts, but (for some unfathomable reason to me) cannot be used as a compile-time constant. `INT_MAX` - is a macro, pretty useless within template functions, but can be used as a compile-time constant. – UncleBens Dec 06 '09 at 14:09
  • 19
    The funny thing is that numeric_limits::max implementation on msvc looks like this: return (INT_MAX); – Nikola Smiljanić Dec 06 '09 at 14:11
  • 13
    @paul Reference for the deprecation please. And guess how numeric_limits implements max()? That's right, "return INT_MAX", at least on GCC 4.4.0. –  Dec 06 '09 at 14:14
  • 2
    @UncleBens: inline functions currently can't be reduced to constant expressions. – Georg Fritzsche Dec 06 '09 at 15:04
  • Yes, I realized it has to be a function in the first place because `numeric_limits` has to be usable for non-integer types as well. – UncleBens Dec 06 '09 at 15:32
  • 1
    @gf: In C++1x they may, using `constexpr`. – dalle Dec 31 '09 at 11:31
  • @dalle But most compilers still do not support constexpr sufficiently. – Crashworks Oct 04 '14 at 00:08
2

Here is a macro I use to get the maximum value for signed integers, which is independent of the size of the signed integer type used, and for which gcc -Woverflow won't complain

#define SIGNED_MAX(x) (~(-1 << (sizeof(x) * 8 - 1)))

int a = SIGNED_MAX(a);
long b = SIGNED_MAX(b);
char c = SIGNED_MAX(c); /* if char is signed for this target */
short d = SIGNED_MAX(d);
long long e = SIGNED_MAX(e);
2

O.K. I neither have rep to comment on previous answer (of Philippe De Muyter) nor raise it's score, hence a new example using his define for SIGNED_MAX trivially extended for unsigned types:

// We can use it to define limits based on actual compiler built-in types also: 
#define INT_MAX   SIGNED_MAX(int)
// based on the above, we can extend it for unsigned types also:
#define UNSIGNED_MAX(x) (  (SIGNED_MAX(x)<<1) | 1 ) // We reuse SIGNED_MAX
#define UINT_MAX  UNSIGNED_MAX(unsigned int) // on ARM: 4294967295
// then we can have:
unsigned int width = UINT_MAX;

Unlike using this or that header, here we use the real type from the compiler.

A. Genchev
  • 369
  • 3
  • 7
2

Why not write a piece of code like:

int  max_neg = ~(1 << 31);
int  all_ones = -1;
int max_pos = all_ones & max_neg;
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Prabhu
  • 3,434
  • 8
  • 40
  • 48
  • 28
    There is no guarantee that int is 32 bits in size and there is no guarantee about the in memory negative integer format. Less importantly, there is no need to make people look up '~'. – Sqeaky Dec 14 '12 at 02:19
1
#include <iostrema>

int main(){
    int32_t maxSigned = -1U >> 1;
    cout << maxSigned << '\n';
    return 0;
}

It might be architecture dependent but it does work at least in my setup.

macmur
  • 29
  • 3