使用climits C++标准头文件可以获取char, short, int, long, long long 等5中基本类型的最大值和最小值
1.编译环境:VS2008
#include <iostream>
#include <iomanip>//needs setw
#include <climits>
using std::cout;
using std::endl;
using std::setw;
using std::ios;
int main()
{
const int COUT_WIDTH = 25;
//char
cout<<"sizeof(char):"<<sizeof(char)<<endl;
cout<<setw(COUT_WIDTH)<<setiosflags(ios::left)<<"char Min:"<<CHAR_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"char Max:"<<CHAR_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"signed char Min:"<<SCHAR_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"signed char Max:"<<SCHAR_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"unsigned char Max:"<<UCHAR_MAX<<endl<<endl;
//short
cout<<"sizeof(short):"<<sizeof(short)<<endl;
cout<<setw(COUT_WIDTH)<<"short Min:"<<SHRT_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"short Max:"<<SHRT_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"unsigned short Max:"<<USHRT_MAX<<endl<<endl;
//int
cout<<"sizeof(int):"<<sizeof(int)<<endl;
cout<<setw(COUT_WIDTH)<<"int Min:"<<INT_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"int Max:"<<INT_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"unsigned int Max:"<<UINT_MAX<<endl<<endl;
//long
cout<<"sizeof(long):"<<sizeof(long)<<endl;
cout<<setw(COUT_WIDTH)<<"long Min:"<<LONG_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"long Max:"<<LONG_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"unsigned long Max:"<<ULONG_MAX<<endl<<endl;
//long long
cout<<"sizeof(long long):"<<sizeof(long long)<<endl;
cout<<setw(COUT_WIDTH)<<"long long Min:"<<LLONG_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"long long Max:"<<LLONG_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"unsigned long long Max:"<<ULLONG_MAX<<endl;
return 0;
}
执行结果:
编译环境:G++
1
2.编译环境:VC++6.0
#include <iostream>
#include <iomanip>//needs setw
#include <climits>
using std::cout;
using std::endl;
using std::setw;
using std::ios;
int main()
{
const int COUT_WIDTH = 25;
//char
cout<<"sizeof(char):"<<sizeof(char)<<endl;
cout.setf(ios::left);
//cout<<setiosflags(ios::left);这样设置对齐方式行不通
cout<<setw(COUT_WIDTH)<<"char Min:"<<CHAR_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"char Max:"<<CHAR_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"signed char Min:"<<SCHAR_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"signed char Max:"<<SCHAR_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"unsigned char Max:"<<UCHAR_MAX<<endl<<endl;
//short
cout<<"sizeof(short):"<<sizeof(short)<<endl;
cout<<setw(COUT_WIDTH)<<"short Min:"<<SHRT_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"short Max:"<<SHRT_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"unsigned short Max:"<<USHRT_MAX<<endl<<endl;
//int
cout<<"sizeof(int):"<<sizeof(int)<<endl;
cout<<setw(COUT_WIDTH)<<"int Min:"<<INT_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"int Max:"<<INT_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"unsigned int Max:"<<UINT_MAX<<endl<<endl;
//long
cout<<"sizeof(long):"<<sizeof(long)<<endl;
cout<<setw(COUT_WIDTH)<<"long Min:"<<LONG_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"long Max:"<<LONG_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"unsigned long Max:"<<ULONG_MAX<<endl<<endl;
/*
//long long 不支持long long类型
cout<<"sizeof(long long):"<<sizeof(long long)<<endl;
cout<<setw(COUT_WIDTH)<<"long long Min:"<<LLONG_MIN<<endl;
cout<<setw(COUT_WIDTH)<<"long long Max:"<<LLONG_MAX<<endl;
cout<<setw(COUT_WIDTH)<<"unsigned long long Max:"<<ULLONG_MAX<<endl;
*/
return 0;
}
执行结果: