立此存照(10)[C++]climits頭文件以及5中基本類型的長度與極值

使用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;
}

執行結果:








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章