#include<iostream> #include<string> #include <limits> using namespace std; int main() { cout << "type: \t\t" << "************size**************"<< endl; cout << "bool: \t\t" << "所占字節數:" << sizeof(bool); cout << "\t最大值:" << (numeric_limits<bool>::max)(); cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl; cout << "char: \t\t" << "所占字節數:" << sizeof(char); cout << "\t最大值:" << (numeric_limits<char>::max)(); cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl; cout << "signed char: \t" << "所占字節數:" << sizeof(signed char); cout << "\t最大值:" << (numeric_limits<signed char>::max)(); cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl; cout << "unsigned char: \t" << "所占字節數:" << sizeof(unsigned char); cout << "\t最大值:" << (numeric_limits<unsigned char>::max)(); cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl; cout << "wchar_t: \t" << "所占字節數:" << sizeof(wchar_t); cout << "\t最大值:" << (numeric_limits<wchar_t>::max)(); cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl; cout << "short: \t\t" << "所占字節數:" << sizeof(short); cout << "\t最大值:" << (numeric_limits<short>::max)(); cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl; cout << "int: \t\t" << "所占字節數:" << sizeof(int); cout << "\t最大值:" << (numeric_limits<int>::max)(); cout << "\t最小值:" << (numeric_limits<int>::min)() << endl; cout << "unsigned: \t" << "所占字節數:" << sizeof(unsigned); cout << "\t最大值:" << (numeric_limits<unsigned>::max)(); cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl; cout << "long: \t\t" << "所占字節數:" << sizeof(long); cout << "\t最大值:" << (numeric_limits<long>::max)(); cout << "\t最小值:" << (numeric_limits<long>::min)() << endl; cout << "unsigned long: \t" << "所占字節數:" << sizeof(unsigned long); cout << "\t最大值:" << (numeric_limits<unsigned long>::max)(); cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl; cout << "double: \t" << "所占字節數:" << sizeof(double); cout << "\t最大值:" << (numeric_limits<double>::max)(); cout << "\t最小值:" << (numeric_limits<double>::min)() << endl; cout << "long double: \t" << "所占字節數:" << sizeof(long double); cout << "\t最大值:" << (numeric_limits<long double>::max)(); cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl; cout << "float: \t\t" << "所占字節數:" << sizeof(float); cout << "\t最大值:" << (numeric_limits<float>::max)(); cout << "\t最小值:" << (numeric_limits<float>::min)() << endl; cout << "size_t: \t" << "所占字節數:" << sizeof(size_t); cout << "\t最大值:" << (numeric_limits<size_t>::max)(); cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl; cout << "string: \t" << "所占字節數:" << sizeof(string) << endl; // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl; cout << "type: \t\t" << "************size**************"<< endl; return 0; }
/*執行結果分析:
以上結果已經非常明確了,一下補充說明幾點:
概念、整型:表示整數、字符和布爾值的算術類型合稱為整型(integral type)。
關於帶符號與無符號類型:整型 int、stort 和 long 都默覺得帶符號型。要獲得無符號型則必須制定該類型為unsigned,比方unsigned long。unsigned int類型能夠簡寫為unsigned,也就是說,unsigned后不加其它類型說明符就意味着是unsigned int。
一字節表示八位,即:1byte = 8 bit;
int: 4byte = 32 bit 有符號signed范圍:2^31-1 ~ -2^31即:2147483647 ~ -2147483648無符號unsigned范圍:2^32-1 ~ 0即:4294967295 ~ 0
long: 4 byte = 32 bit 同int型
double: 8 byte = 64 bit 范圍:1.79769e+308 ~ 2.22507e-308
long double: 12 byte = 96 bit 范圍: 1.18973e+4932 ~ 3.3621e-4932
float: 4 byte = 32 bit 范圍: 3.40282e+038 ~ 1.17549e-038
int、unsigned、long、unsigned long 、double的數量級最大都僅僅能表示為10億,即它們表示十進制的位數不超過10個,即能夠保存全部9位整數。而short僅僅是能表示5位;
另外對於浮點說而言:使用double類型基本上不會有錯。在float類型中隱式的精度損失是不能忽視的,二雙精度計算的代價相對於單精度能夠忽略。其實,在有些機器上,double類型比float類型的計算要快得多。float型僅僅能保證6位有效數字,而double型至少能夠保證15位有效數字(小數點后的數位),long double型提供的精度通常沒有必要,並且還要承擔額外的執行代價。
double是8字節共64位,當中小數位占52位,2-^52=2.2204460492503130808472633361816e-16,量級為10^-16,故可以保證2^-15的全部精度。
在有些機器上,用long類型進行計算所付出的執行時代價遠遠高於用int類型進行相同計算的代價,所以算則類型前要先了解程序的細節而且比較long類型與int類型的實際執行時性能代價。
歡迎提出寶貴意見,以幫助我改進,不勝感激!!!
——桑海整理