Search code examples
c++cstllimitsnumeric-limits

maximum value of int


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


Solution

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