項目中用到long、long long等字段,遇到一些問題。先說獲得的一些結論:ios
本身寫了程序測試各平臺下(Windows、Linux、AIX),C++各基本類型的最大值、最小值,以及佔用字節數。測試
#include <iostream> #include <limits.h> using namespace std; void checkEndian(); int main() { checkEndian(); cout<<"程序位數="<<sizeof(void*)*8<<endl; cout<<endl<<"各類數據類型:"<<endl; cout<<"sizeof(char)="<<sizeof(char)<<endl; cout<<"sizeof(int)="<<sizeof(int)<<endl; cout<<"sizeof(unsigned int)="<<sizeof(unsigned int)<<endl; cout<<"sizeof(short)="<<sizeof(short)<<endl; cout<<"sizeof(long)="<<sizeof(long)<<endl; cout<<"sizeof(unsigned long)="<<sizeof(unsigned long)<<endl; cout<<"sizeof(long long)="<<sizeof(long long)<<endl; cout<<"sizeof(unsigned long long)="<<sizeof(long long)<<endl; cout<<"sizeof(float)="<<sizeof(float)<<endl; cout<<"sizeof(double)="<<sizeof(double)<<endl; cout<<endl<<"各類數據類型的最大、最小數:"<<endl; cout<<"min(int)="<<INT_MIN<<endl; cout<<"max(int)="<<INT_MAX<<endl; cout<<"min(unsigned int)="<<0<<endl; cout<<"max(unsigned int)="<<UINT_MAX<<endl; cout<<"min(long)="<<LONG_MIN<<endl; cout<<"max(long)="<<LONG_MAX<<endl; cout<<"min(unsigned long)="<<0<<endl; cout<<"max(unsigned long)="<<ULONG_MAX<<endl; cout<<"min(long long)="<<LLONG_MIN<<endl; cout<<"max(long long)="<<LLONG_MAX<<endl; cout<<"min(unsigned long long)="<<0<<endl; cout<<"max(unsigned long long)="<<ULLONG_MAX<<endl; cout<<endl<<"各類數據類型的指針:"<<endl; cout<<"sizeof(void*)="<<sizeof(void*)<<endl; cout<<"sizeof(char*)="<<sizeof(char*)<<endl; cout<<"sizeof(int*)="<<sizeof(int*)<<endl; cout<<"sizeof(short*)="<<sizeof(short*)<<endl; cout<<"sizeof(long*)="<<sizeof(long*)<<endl; cout<<"sizeof(float*)="<<sizeof(float*)<<endl; cout<<"sizeof(double*)="<<sizeof(double*)<<endl; return 0; } void checkEndian() { short int a=0x1122; char* b = (char*)&a; if(*b==0x11) { cout<<"大小端模式:大端"<<endl; } else { cout<<"大小端模式:小端"<<endl; } }
編譯環境:spa
測試結果:
指針
從以上測試結果,能夠總結幾點:code