Stdnumeric Limitsmax And Stdnumeric Limitsmin In C++ - C++ Programming Tutorial
C++ Course / Advanced Topics / Stdnumeric Limitsmax And Stdnumeric Limitsmin In C++

Stdnumeric Limitsmax And Stdnumeric Limitsmin In C++

BLUF: Mastering Stdnumeric Limitsmax And Stdnumeric Limitsmin In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Stdnumeric Limitsmax And Stdnumeric Limitsmin In C++

C++ is renowned for its efficiency. Learn how Stdnumeric Limitsmax And Stdnumeric Limitsmin In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore the functions std::numericlimits::max and std::numericlimits::min, including their syntax and usage examples.

What is std::numeric_limits::max?

The std::numeric_limits<T>:: max function provides the highest finite value that can be stored using the numeric data type T. This method is compatible with all numerical data types for 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

Argument: It has the ability to accept a variety of data types, like T.

Depending on the data type T, the function will return either predefined macros, the boolean value true, or the default value of T.

Program 1:

The subsequent program showcases the function 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 function provides the minimum finite value that can be represented by the numeric data type T. Any bounded arithmetic type is acceptable for type T.

Header File:

Example

#include<limits>

Syntax:

It has the following syntax:

Example

std::numeric_limits<T>::min

Argument: It has the capability to accept various forms of data, like T.

Depending on the type T, this function returns predefined macros, true, or the default value T. The minimum result is the smallest positive normalized value for floating-point number types with denormalization. When working with floating-point data types, you can utilize numeric_limits::lowest to determine the value that does not have any smaller values 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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience