Skip to content
Related Articles
Open in App
Not now

Related Articles

std::numeric_limits::max() and std::numeric_limits::min() in C++

Improve Article
Save Article
  • Last Updated : 01 Feb, 2021
Improve Article
Save Article

std::numeric_limits::max():

The std::numeric_limits<T>::max() function is used to get the maximum finite value representable by the numeric type T. All arithmetic types are valid for type T.

Header File:

#include<limits>

Template:

static T max() throw();
static constexpr T max() noexcept;

Syntax:

std::numeric_limits<T>::max

Parameter: It receives any one type of data type i.e., T.

Return Type: It returns predefined macros, true or default T() depending on type T. Some standard return values are:

Type Tstd::numeric_limits<T>::max()
/* non-specialized */T()
boolTRUE
charCHAR_MAX
signed charSCHAR_MAX
unsigned charUCHAR_MAX
wchar_tWCHAR_MAX
shortSHRT_MAX
unsigned shortUSHRT_MAX
intINT_MAX
unsigned intUINT_MAX
longLONG_MAX
unsigned longULONG_MAX
long longLLONG_MAX
unsigned long longULLONG_MAX
floatFLT_MAX
doubleDBL_MAX
long doubleLDBL_MAX

Program 1:

Below is the program to illustrate the function std::numeric_limits<T>::max(). The output of the program is system-specific.

C++




// C++ program to illustrate the
// function numeric_limits<T>::max
#include <iostream>
#include <limits>
using namespace std;
  
// Driver Code
int main()
{
    cout << "bool: "
         << numeric_limits<bool>::max()
         << '\n';
  
    // It returns 127 in ASCII value
    // to print in integer that can
    // be typecast it to int()
    cout << "char: "
         << int(numeric_limits<char>::max())
         << '\n';
  
    cout << "unsigned char: "
         << int(numeric_limits<unsigned char>::max())
         << '\n';
  
    cout << "short: "
         << numeric_limits<short>::max()
         << '\n';
  
    cout << "int: " << numeric_limits<int>::max()
         << '\n';
  
    cout << "unsigned int: "
         << numeric_limits<unsigned int>::max()
         << '\n';
  
    cout << "long long: "
         << numeric_limits<long long>::max()
         << '\n';
  
    cout << "float: "
         << numeric_limits<float>::max()
         << '\n';
  
    cout << "double: "
         << numeric_limits<double>::max()
         << '\n';
  
    cout << "size_t: "
         << numeric_limits<size_t>::max()
         << '\n';
}

Output:

bool: 1
char: 127
unsigned char: 255
short: 32767
int: 2147483647
unsigned int: 4294967295
long long: 9223372036854775807
float: 3.40282e+38
double: 1.79769e+308
size_t: 18446744073709551615

std::numeric_limits::min():

The std::numeric_limits<T>::min() function is used to get the minimum finite value representable by the numeric type T. All bounded arithmetic types are valid for type T.

Header File:

#include<limits>

Template:

static T min() throw();
static constexpr T min() noexcept;

Syntax:

std::numeric_limits<T>::min

Parameter: It receives any one type of data type i.e., T.

Return Type: It returns predefined macros, true or default T() depending on type T. For floating-point types with denormalization, min returns the minimum positive normalized value. To find the value that has no values less than it for floating data type, use numeric_limits::lowest().  Some standard return values are:

Type Tstd::numeric_limits::min()
/* non-specialized */T()
boolFALSE
charCHAR_MIN
signed charSCHAR_MIN
unsigned char​0​
wchar_tWCHAR_MIN
shortSHRT_MIN
unsigned short0
intINT_MIN
unsigned int0
longLONG_MIN
unsigned long0
long longLLONG_MIN
unsigned long long​0​
floatFLT_MIN
doubleDBL_MIN
long doubleLDBL_MIN

Program 2:

Below is the program to illustrate the function std::numeric_limits<T>::min(). The output of the program is system-specific.

C++




// C++ program to illustrate the
// function numeric_limits<T>::min
#include <iostream>
#include <limits>
using namespace std;
  
// Driver Code
int main()
{
    cout << "bool: "
         << numeric_limits<bool>::min()
         << '\n';
  
    // numeric_limits<char>:: min()
    // returns 127 in ASCII value in
    // integer that can be typecast
    // to int()
    cout << "char: "
         << int(numeric_limits<char>::min())
         << '\n';
  
    cout << "unsigned char: "
         << int(numeric_limits<unsigned char>::min())
         << '\n';
  
    cout << "short: "
         << numeric_limits<short>::min() << '\n';
  
    cout << "int: " << std::numeric_limits<int>::min()
         << '\n';
    cout << "unsigned int: "
         << numeric_limits<unsigned int>::min()
         << '\n';
  
    cout << "long long: "
         << numeric_limits<long long>::min()
         << '\n';
    cout << "float: "
         << numeric_limits<float>::min()
         << '\n';
  
    cout << "double: "
         << numeric_limits<double>::min()
         << '\n';
  
    cout << "size_t: "
         << numeric_limits<size_t>::min()
         << '\n';
}

Output:

bool: 0
char: -128
unsigned char: 0
short: -32768
int: -2147483648
unsigned int: 0
long long: -9223372036854775808
float: 1.17549e-38
double: 2.22507e-308
size_t: 0

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!