Tuesday, March 16, 2010

Maximum and minimum limit for numeric data type



Maximum and minimum value of numeric data type

In c++, the numeric data types are short, int, long, float, double and long double. Every data type has certain limits. There is minimum and maximum value that it can hold. If a variable of a given data type is assigned a value that is greater than the maximum limit or smaller than the minimum limit, it results into wrong output. This mistake does not produce compiler error or runtime error. Hence we should be careful while choosing data type for a variable. The below is a c++ program which is used to determine the maximum and minimum values that data type can hold.

A c++ program to determine the maximum and minimum values of numeric data types.


#include <iostream>
#include <limits>

using std::cout;
using std::endl;
using std::numeric_limits;

int main ()
{
    cout << "The values for data type short ranges from: "
            << numeric_limits<short>::min () << " to "
            << numeric_limits<short>::max () << endl;

    cout << "The values for the data type int ranges from: "
            << numeric_limits<int>::min () << " to "
            << numeric_limits<int>::max () << endl;

    cout << "The values for the data type long ranges from: "
            << numeric_limits<long>::min () << " to "
            << numeric_limits<long>::max () << endl;

    cout << "The values for the data type float ranges from: "
            << numeric_limits<float>::min () << " to "
            << numeric_limits<float>::max () << endl;

    cout << "The values for the data type double ranges from: "
            << numeric_limits<double>::min () << " to "
            << numeric_limits<double>::max () << endl;

    cout << "The values for the data type long double ranges from: "
            << numeric_limits<long double>::min () << " to "
            << numeric_limits<long double>::max () << endl;

    return 0;
}

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters

2 comments: