The C + + standard library provides limit values for numeric types through template numeric_limits. Using this template, you can improve the portability of the program to a certain extent.
#include <iostream> #include <limits>using std::cout;using std::endl;using std::numeric_limits;int Main () {cout<< "The min of char is:" << (int) numeric_limits<char>::min () <<endl; cout<< "The Max of char is:" << (int) Numeric_limits<char>::max () <<endl; cout<< "The Min of short is:" <<numeric_limits<short>::min () <<endl; cout<< "The max of short is:" <<numeric_limits<short>::max () <<endl; cout<< "The min of int is:" <<numeric_limits<int>::min () <<endl; cout<< "The max of int is:" <<numeric_limits<int>::max () <<endl; cout<< "The Min of Long is:" <<numeric_limits<long>::min () <<endl; cout<< "The max of Long is:" <<numeric_limits<long>::max () <<endl; cout<< "The Min of float is:" <<numeric_limits<float>::min () <<endl; cout<< "The max of float is:" <<numeriC_limits<float>::max () <<endl; cout<< "The Min of double is:" <<numeric_limits<double>::min () <<endl; cout<< "The max of Double is:" <<numeric_limits<double>::max () <<endl; cout<< "The min of long double is:" <<numeric_limits<long double>::min () <<endl; cout<< "The max of long Double is:" <<numeric_limits<long Double>::max () <<endl; cout<< "The min of unsigned is:" <<numeric_limits<unsigned>::min () <<endl; cout<< "The max of unsigned is:" <<numeric_limits<unsigned>::max () <<endl;}
Operation Result:
[Email protected] cpp]$ g++-o 4.5./4.5.cpp[[email protected] cpp]$./4.5 the Min of char is: -128 the max of Char Is:1 The Min of is: -32768 the max of is:32767 the min of int is: -2147483648 the max of int is:2147483647 the Min of Long is: -9223372036854775808 the max of Long is:9223372036854775807 the min of float is:1.17549e-38 the max of Float is:3.40282e+38 the min of double is:2.22507e-308 the max of double is:1.79769e+308 the min of long double is:3.3 621e-4932 the max of long double is:1.18973e+4932 the min of unsigned is:0 the max of unsigned is:4294967295
C + + programming language Exercise 4.5