C++数据类型大小

mac2024-03-20  25

#include<climits> int main() { using namespace std; int n_int=INT_MAX; short n_short=SHRT_MAX; long n_long=LONG_MAX; long long n_llong=LLONG_MAX; // float is 4 bytes. // double is 8 bytes. cout<<"char is "<<sizeof(char)<<" bytes."<<endl; cout<<"unsigned char is "<<sizeof(unsigned char)<<" bytes."<<endl; cout<<"short is "<<sizeof n_short<<" bytes."<<endl; cout<<"unsigned short is "<<sizeof(unsigned short)<<" bytes."<<endl; cout<<"int is "<<sizeof(int)<<" bytes."<<endl; cout<<"unsigned is "<<sizeof(unsigned)<<" bytes."<<endl; cout<<"long is "<<sizeof n_long<<" bytes."<<endl; cout<<"long long is "<<sizeof n_llong<<" bytes."<<endl; cout<<endl; cout<<"Maximum values"<<endl; cout<<"char : "<<CHAR_MAX<<endl; cout<<"U char : "<<UCHAR_MAX<<endl; cout<<"int : "<<n_int<<endl; cout<<"U int : "<<UINT_MAX<<endl; cout<<"short : "<<n_short<<endl; cout<<"U SHORT : "<<USHRT_MAX<<endl; cout<<"long : "<<n_long<<endl; cout<<"long long : "<<n_llong<<endl<<endl; cout<<"Minimum values"<<endl; cout<<"char : "<<CHAR_MIN<<endl; cout<<"int : "<<INT_MIN<<endl; cout<<"short : "<<SHRT_MIN<<endl; cout<<"long : "<<LONG_MIN<<endl; cout<<"long long : "<<LLONG_MIN<<endl<<endl; cout<<"Bits per byte = "<<CHAR_BIT<<endl; return 0; }

ubuntu系统下输出结果:

char           is 1 bytes. unsigned  char is 1 bytes. short          is 2 bytes. unsigned short is 2 bytes. int            is 4 bytes. unsigned       is 4 bytes. long           is 8 bytes. // ----windows下是4 long long      is 8 bytes.  Maximum values char          : 127 U char        : 255 int           : 2147483647 U int         : 4294967295 short         : 32767 U SHORT       : 65535 long          : 9223372036854775807//-----windows下是 2147483647 long long     : 9223372036854775807  Minimum values char          : -128 int           : -2147483648 short         : -32768 long          : -9223372036854775808//-----windows下是 2147483648 long long     : -9223372036854775808  Bits per byte    = 8

 

最新回复(0)