Get Minimum and Maximum Values of Data Types using C++

Get Minimum and Maximum Values of Data Types using C++

C++ has several built-in data types that are used to store different kinds of values. Each data type has a specific range of values that it can hold. The minimum and maximum values of data types are dependent on the system and the compiler being used. This tutorial demonstrates how to get the minimum and maximum values of data types using C++.

The limits header file can be used to determine the characteristics of the various data types. The numeric_limits template class defines a set of member functions that return the minimum and maximum values of a given data type. The following example prints to the console the minimum and maximum values of each data type.

#include <iostream>
#include <limits>

template<typename T>
void printMinMax(const char* type)
{
    std::cout << type;
    std::cout << ", min: " << std::numeric_limits<T>::min();
    std::cout << ", max: " << std::numeric_limits<T>::max() << std::endl;
}

template<typename T>
void printCharMinMax(const char* type)
{
    std::cout << type;
    std::cout << ", min: " << (int) std::numeric_limits<T>::min();
    std::cout << ", max: " << (int) std::numeric_limits<T>::max() << std::endl;
}

int main()
{
    printMinMax<bool>("bool");
    printCharMinMax<char>("char");
    printCharMinMax<unsigned char>("unsigned char");
    printMinMax<wchar_t>("wchar_t");
    printMinMax<short>("short");
    printMinMax<unsigned short>("unsigned short");
    printMinMax<int>("int");
    printMinMax<unsigned int>("unsigned int");
    printMinMax<long>("long");
    printMinMax<unsigned long>("unsigned long");
    printMinMax<long long>("long long");
    printMinMax<unsigned long long>("unsigned long long");
    printMinMax<float>("float");
    printMinMax<double>("double");
    printMinMax<long double>("long double");

    return 0;
}

Here's an example of the output:

bool, min: 0, max: 1
char, min: -128, max: 127
unsigned char, min: 0, max: 255
wchar_t, min: -2147483648, max: 2147483647
short, min: -32768, max: 32767
unsigned short, min: 0, max: 65535
int, min: -2147483648, max: 2147483647
unsigned int, min: 0, max: 4294967295
long, min: -9223372036854775808, max: 9223372036854775807
unsigned long, min: 0, max: 18446744073709551615
long long, min: -9223372036854775808, max: 9223372036854775807
unsigned long long, min: 0, max: 18446744073709551615
float, min: 1.17549e-38, max: 3.40282e+38
double, min: 2.22507e-308, max: 1.79769e+308
long double, min: 3.3621e-4932, max: 1.18973e+4932

It is important to note that the output may be different on other systems.

The 2 Comments Found

  1. Avatar
    Manohar Mathur Reply

    Sir,
    The ranges of long and long long are the same as shown below:
    long, min: -9223372036854775808, max: 9223372036854775807
    long long, min: -9223372036854775808, max: 9223372036854775807.
    My question is why long long and long has the same range?
    Thanks,

    • Avatar
      lindevs Reply

      Hi,
      In C++, the size and range of integer types, including long and long long, are implementation-dependent. For many systems, both long and long long are often implemented as 64-bit integers. In such cases, they would have the same range, as you observed.
      The reason for having different integer types like long and long long is to provide flexibility for different platforms and to allow compilers to choose the most efficient representation for a given system. Some systems may have long as a 32-bit type, and long long as a 64-bit type. In those cases, long would have a smaller range compared to long long.

Leave a Comment

Cancel reply

Your email address will not be published.