C++标准库通过template numeric_limits提供数值类型的极限值。使用这个模板,可以一定程度的提高程序的可移植性。
#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;
}
运行结果:
[chaos@localhost cpp]$ g++ -o 4.5 ./4.5.cpp
[chaos@localhost cpp]$ ./4.5
the min of char is: -128
the max of char is: 127
the min of short is: -32768
the max of short 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.3621e-4932
the max of long double is: 1.18973e+4932
the min of unsigned is: 0
the max of unsigned is: 4294967295