Stdnumeric Limitsmax And Stdnumeric Limitsmin In C++

In this article, we will discuss std::numericlimits::max and std::numericlimits::min functions with their syntax and examples.

What is std::numeric_limits::max?

The std::numeric_limits<T>:: max method returns the largest finite number that can be represented by the numeric type T. All arithmetic types can work for type T.

Header files:

Example

#include<limits>

Template:

Example

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

Syntax:

It has the following syntax:

Example

std::numeric_limits<T>::max

Parameter: It can receive any single type of data, such as T.

Return Type: Depending on the type T, it returns predefined macros, true, or the default T.

Program 1:

The following program demonstrates the method std::numeric_limits<T>::max.

Filename: MaxLimits.cpp

Example

// program to implement the numeric std--numeric_limits--max() function
#include <limits> 
#include <iostream> 
using namespace std; 
int main() 
{ 
	cout << "bool limit: "
		<< numeric_limits<bool>::max() 
		<< '\n'; 

	//It prints the integer value of type cast
	cout << "char limit: "
		<< int(numeric_limits<char>::max()) 
		<< '\n'; 

	cout << "unsigned char limit: "
		<< int(numeric_limits<unsigned char>::max()) 
		<< '\n'; 

	cout << "short limit: "
		<< numeric_limits<short>::max() 
		<< '\n'; 

	cout << "int: " << numeric_limits<int>::max() 
		<< '\n'; 

	cout << "unsigned int limit: "
		<< numeric_limits<unsigned int>::max() 
		<< '\n'; 

	cout << "long long limit: "
		<< numeric_limits<long long>::max() 
		<< '\n'; 

	cout << "float limit: "
		<< numeric_limits<float>::max() 
		<< '\n'; 

	cout << "double limit: "
		<< numeric_limits<double>::max() 
		<< '\n'; 

	cout << "size_t limit: "
		<< numeric_limits<size_t>::max() 
		<< '\n'; 
}

Output:

Output

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

What is std::numeric_limits::min?

The std::numeric_limits<T>::min method returns the smallest finite number that may be represented by the numeric type T. For type T, any limited arithmetic type is valid.

Header File:

Example

#include<limits>

Syntax:

It has the following syntax:

Example

std::numeric_limits<T>::min

Parameter: It can receive any single type of data, such as T.

Return Type: Depending on the type T, it returns established macros, true or default T. Min results in the smallest positive normalized value for floating-point numbers types with denormalization. For the floating data type, use numeric_limits::lowest to identify the value that contains no values smaller than it.

Example:

Example

// Program to implement the  std--numeric_limits--min() in C++
#include <iostream> 
#include <limits> 
using namespace std; 
int main() 
{ 
	cout << "bool min value: "
		<< numeric_limits<bool>::min() 
		<< '\n'; 

	// numeric_limits<char>:: min() // results in 127 in ASCII in an integer that may be typecast to int() 
	cout << "char min value: "
		<< int(numeric_limits<char>::min()) 
		<< '\n'; 

	cout << "unsigned char min value: "
		<< int(numeric_limits<unsigned char>::min()) 
		<< '\n'; 

	cout << "short min value: "
		<< numeric_limits<short>::min() << '\n'; 

	cout << "int min value: " << std::numeric_limits<int>::min() 
		<< '\n'; 
	cout << "unsigned int min value: "
		<< numeric_limits<unsigned int>::min() 
		<< '\n'; 

	cout << "long long min value: "
		<< numeric_limits<long long>::min() 
		<< '\n'; 
	cout << "float: "
		<< numeric_limits<float>::min() 
		<< '\n'; 

	cout << "double min value: "
		<< numeric_limits<double>::min() 
		<< '\n'; 

	cout << "size_t min value: "
		<< numeric_limits<size_t>::min() 
		<< '\n'; 
}

Output:

Output

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

Input Required

This code uses input(). Please provide values below: