std::numeric_limits::max() and std::numeric_limits::min() in C++
Last Updated : 01 Feb, 2021
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 T | std::numeric_limits<T>::max() |
---|
/* non-specialized */ | T() |
bool | TRUE |
char | CHAR_MAX |
signed char | SCHAR_MAX |
unsigned char | UCHAR_MAX |
wchar_t | WCHAR_MAX |
short | SHRT_MAX |
unsigned short | USHRT_MAX |
int | INT_MAX |
unsigned int | UINT_MAX |
long | LONG_MAX |
unsigned long | ULONG_MAX |
long long | LLONG_MAX |
unsigned long long | ULLONG_MAX |
float | FLT_MAX |
double | DBL_MAX |
long double | LDBL_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 T | std::numeric_limits::min() |
---|
/* non-specialized */ | T() |
bool | FALSE |
char | CHAR_MIN |
signed char | SCHAR_MIN |
unsigned char | 0 |
wchar_t | WCHAR_MIN |
short | SHRT_MIN |
unsigned short | 0 |
int | INT_MIN |
unsigned int | 0 |
long | LONG_MIN |
unsigned long | 0 |
long long | LLONG_MIN |
unsigned long long | 0 |
float | FLT_MIN |
double | DBL_MIN |
long double | LDBL_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
Similar Reads
Difference between std::numeric_limits<T> min, max, and lowest in C++ The std::numeric_limits<T> class in the limit header provides min(), max(), and lowest() function for all numeric data types along with the other member functions. std::numeric_limits<T>::max(): The std::numeric_limits<T>::max() for any type T gives the maximum finite value represe
5 min read
Find Maximum and Minimum Element in a Set in C++ STL In C++, set stores the unique elements in sorted order so it is pretty straightforward to find the minimum and maximum values. In this article, we will learn different methods to find the minimum and maximum values in a set in C++.As the set is sorted, the most efficient way to find the minimum and
3 min read
Data types that supports std::numeric_limits() in C++ The numeric_limits class template provides an easy and standardized way to query various properties of arithmetic types. For example, the maximum value a type T can store is std::numeric_limits<T>::max(). Example: std::numeric_limits<int>::max() gives the maximum possible value we can st
8 min read
How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma
2 min read
How to Find the Range of Numbers in an Array in C++? The range of numbers in an array means the difference between the maximum value and the minimum value in the given array. In this article, we will learn how to find the range of numbers in an array in C++. For Example, Input: myArray = {5,10,15,30,25}; Output: Range: 25Range Within an Array in C++To
2 min read