Table of Contents
Each fundamental data type can store values only within a specific range. The <limits> header provides information about these ranges through the std::numeric_limits class template.
This example demonstrates how to determine the minimum and maximum values that can be stored by commonly used numeric data types.
Source Code #
#include <iostream>
#include <limits>
int main()
{
std::cout << "int\n";
std::cout << "Minimum: " << std::numeric_limits<int>::min() << '\n';
std::cout << "Maximum: " << std::numeric_limits<int>::max() << "\n\n";
std::cout << "float\n";
std::cout << "Minimum: " << std::numeric_limits<float>::lowest() << '\n';
std::cout << "Maximum: " << std::numeric_limits<float>::max() << '\n';
return 0;
}
Output #
int
Minimum: -2147483648
Maximum: 2147483647
float
Minimum: -3.40282e+38
Maximum: 3.40282e+38
The reported values are implementation-dependent and may vary depending on the compiler and target platform.
Explanation #
#include <limits>includes the Standard Library header that defines thestd::numeric_limitsclass template.std::numeric_limits<int>::min()returns the smallest value that can be represented by theinttype.std::numeric_limits<int>::max()returns the largest value that can be represented by theinttype.std::numeric_limits<float>::lowest()returns the lowest finite value that can be represented by thefloattype.std::numeric_limits<float>::max()returns the largest finite value that can be represented by thefloattype.std::numeric_limitsprovides similar information for most fundamental arithmetic types, includingchar,short,long,double, andbool.return 0;terminates the program successfully.