现在的位置: 首页 > 综合 > 正文

32/64位机器 数据类型最大值测试

2018年01月18日 ⁄ 综合 ⁄ 共 7711字 ⁄ 字号 评论关闭

gcc版本

Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)

 

/*.int数据类型int类型数据所占内存空间为32位。
其中有符号整型变量取值范围为-2147483648~2147483647,
无符号型整型变量取值范围为0~4294967295U.
*/
int ia = 2147483647;//int 最大值不越界
int iaOver = 2147483647+1;//越界
//32 64位编译结果一样
cout <<"ia不越界:"<<ia<<endl;//ia不越界:2147483647  
cout <<"iaOver越界:"<<iaOver<<endl;//iaOver越界:-2147483648
//32 位编译unsigned int 
unsigned int uia = 4294967295;
unsigned int uiau = 4294967295u;
unsigned int uiOver = 4294967295+1;
unsigned int uiOveru = 4294967295+1u;
cout <<"uia不越界:"<<uia<<endl;//4294967295
cout <<"uiau不越界:"<<uiau<<endl;//4294967295
cout <<"uiOver越界:"<<uiOver<<endl;//0
cout <<"uiOver越界:"<<uiOveru<<endl;//0  */
/*32编译结果
int_long_32_64.cpp:17: warning: this decimal constant is unsigned only in ISO C90
int_long_32_64.cpp:19: warning: this decimal constant is unsigned only in ISO C90
int_long_32_64.cpp:20: warning: this decimal constant is unsigned only in ISO C90
*/
//64 位编译unsigned int 
unsigned int uia = 4294967295;
unsigned int uiau = 4294967295u;
unsigned int uiOver = 4294967295+1;
unsigned int uiOveru = 4294967295+1u;
cout <<"uia不越界:"<<uia<<endl;//4294967295
cout <<"uiau不越界:"<<uiau<<endl;//4294967295
cout <<"uiOver越界:"<<uiOver<<endl;//0
cout <<"uiOver越界:"<<uiOveru<<endl;//0
//64编译结果
/*int_long_32_64.cpp:34: warning: large integer implicitly truncated to unsigned type
int_long_32_64.cpp:35: warning: large integer implicitly truncated to unsigned type
*/

 

//long int数据类型 
/*
随着宏__WORDSIZE值的改变,long int数据类型的大小也会发生改变。
如果__WORDSIZE的值为32,则long int和int类型一样,占有32位。
在Linux GCC4.0-i386版本中,默认情况下__WORDSIZE的值为32.其定义如下:
#include</usr/include/bits/wordsize.h>
#define __WORDSIZE 32 
在64位机器上,如果__WORDSIZE的值为64, 
long int类型数据所占内存空间为64位。
其中有长整型变量取值范围为-9223372036854775808L~9223372036854775807L,
无符号长整型变量取值范围为0~18446744073709551615UL.其限制如下: 
*/
//32 位编译long 
cout<<"查看机器编译器位数:"<<__WORDSIZE<<endl;//32
long la = 2147483647;
long laOver = 2147483647+1;
long lal = 2147483647l;
long laOverl = 2147483647+1l;
cout <<"la不越界:"<<la<<endl;//2147483647
cout <<"laOver越界:"<<laOver<<endl;//-2147483648
cout <<"lal不越界:"<<lal<<endl;//0
cout <<"laOverl越界:"<<laOverl<<endl;//-2147483648
//32编译结果
int_long_32_64.cpp: In function ‘int main()’:
int_long_32_64.cpp:663: warning: overflow in implicit constant conversion
//64 位编译long 
cout<<"查看机器编译器位数:"<<__WORDSIZE<<endl;//64
long la = 2147483647;
long laOver = 2147483647+1;
long lal = 2147483647l;
long laOverl = 2147483647+1l;
cout <<"la不越界:"<<la<<endl;//2147483647
cout <<"laOver越界:"<<laOver<<endl;//-2147483648 数据末尾未加l(小写L)系统默认为int越界
cout <<"lal不越界:"<<lal<<endl;//0
cout <<"laOverl越界:"<<laOverl<<endl;//2147483648 64位机器此处不越界
//64 位编译结果
int_long_32_64.cpp: In function ‘int main()’:
int_long_32_64.cpp:78: warning: overflow in implicit constant conversion
//由于64位的编译器long占8*8位所以将long的最大值进行测试
/*
在64位机器上,如果__WORDSIZE的值为64, long int类型数据所占内存空间为64位。
其中有长整型变量取值范围为-9223372036854775808L~9223372036854775807L,
无符号长整型变量取值范围为0~18446744073709551615UL.
*/
cout<<"查看机器编译器位数:"<<__WORDSIZE<<endl;//64
long la64 = 9223372036854775807;
long laOver64 = 9223372036854775807+1;
long lal64 = 9223372036854775807l;
long laOverl64 = 9223372036854775807+1l;
cout <<"la64不越界:"<<la64<<endl;//9223372036854775807
cout <<"laOver64越界:"<<laOver64<<endl;//-9223372036854775808 
cout <<"lal64不越界:"<<lal64<<endl;//9223372036854775807
cout <<"laOverl64越界:"<<laOverl64<<endl;//-9223372036854775808 
//64编译 无问题
//32位编译
/*全部越界
int_long_32_64.cpp:97: warning: overflow in implicit constant conversion
int_long_32_64.cpp:98: warning: overflow in implicit constant conversion
int_long_32_64.cpp:99: warning: overflow in implicit constant conversion
int_long_32_64.cpp:100: warning: overflow in implicit constant conversion
*/

//32 unsinged long 
/*.int/long数据类型int/long类型数据所占内存空间为32位。
无符号型整型变量取值范围为0~4294967295U.
*/
unsigned long ul32 = 4294967295;
unsigned long ulaOver32 = 4294967295u;
unsigned long ulal32 = 4294967295+1;
unsigned long ulaOverl32 = 4294967295+1u;
cout <<"ul32不越界:"<<ul32<<endl;//4294967295
cout <<"ulaOver32不越界:"<<ulaOver32<<endl;//4294967295
cout <<"ulal32越界:"<<ulal32<<endl;//0
cout <<"ulaOverl32越界:"<<ulaOverl32<<endl;//0

/*32编译结果
int_long_32_64.cpp:122: warning: this decimal constant is unsigned only in ISO C90
int_long_32_64.cpp:124: warning: this decimal constant is unsigned only in ISO C90
int_long_32_64.cpp:125: warning: this decimal constant is unsigned only in ISO C90
*/

 

//64 unsinged long 
//无符号长整型变量取值范围为0~18446744073709551615UL.
//其中有长整型变量取值范围为-9223372036854775808L~9223372036854775807L,
unsigned long ul64 = 4294967295;
unsigned long ulaOver64 = 4294967295u;
unsigned long ulal64 = 4294967295+1;
unsigned long ulaOverl64 = 4294967295+1u;
cout <<"ul64不越界:"<<ul64<<endl;//4294967295
cout <<"ulaOver64不越界:"<<ulaOver64<<endl;//4294967295
cout <<"ulal64不越界:"<<ulal64<<endl;//4294967296
cout <<"ulaOverl64不越界:"<<ulaOverl64<<endl;//4294967296
/*64编译结果
正常无错
*/
unsigned long ula64_2 = 18446744073709551615;
unsigned long ula64_2u = 18446744073709551615u;
unsigned long ula64Over_2 = 18446744073709551615+1;
unsigned long ula64Over_2u = 18446744073709551615+1u;
cout <<"ula64_2不越界:"<<ula64_2<<endl;//18446744073709551615
cout <<"ula64_2u不越界:"<<ula64_2u<<endl;//18446744073709551615                                           
cout <<"ula64Over_2越界:"<<ula64Over_2<<endl;//0
cout <<"ula64Over_2u越界:"<<ula64Over_2u<<endl;//0
//64编译结果
int_long_32_64.cpp:149:25: warning: integer constant is so large that it is unsigned
int_long_32_64.cpp:149: warning: this decimal constant is unsigned only in ISO C90
int_long_32_64.cpp:151:29: warning: integer constant is so large that it is unsigned
int_long_32_64.cpp:151: warning: this decimal constant is unsigned only in ISO C90
int_long_32_64.cpp:152:30: warning: integer constant is so large that it is unsigned
int_long_32_64.cpp:152: warning: this decimal constant is unsigned only in ISO C90
*/

//32位 long long 

cout<<"32位编译器 long long占:"<<sizeof(long long)<<endl;//8
//32位的long long 与64位的long 最大数值一样
//取值范围为-9223372036854775808L~9223372036854775807LL,
long long lla32 = 9223372036854775807ll;//不加ll错误 过不去
//long long llaOver32 = 9223372036854775807+1ll;//编译错误 过不去
cout <<"lla32不越界:"<<lla32<<endl;//9223372036854775807
//cout <<"llaOver32越界:"<<llaOver32<<endl;//
//64位 long long 
cout<<"64位编译器 long long占:"<<sizeof(long long)<<endl;//8
//32位的long long 与64位的long 最大数值一样
//取值范围为-9223372036854775808L~9223372036854775807LL,
long long lla32 = 9223372036854775807;//9223372036854775807ll
long long llaOver32 = 9223372036854775807+1;//9223372036854775807+1ll
cout <<"lla32不越界:"<<lla32<<endl;//9223372036854775807
cout <<"llaOver32越界:"<<llaOver32<<endl;//-9223372036854775808
//64位编译结果
//数据结尾加不加ll都一样,可通过
//32位 unsigned long long 
cout<<"32位编译器 unsigned long long占:"<<sizeof(unsigned long long)<<endl;//8
unsigned long long ulla32 = 18446744073709551615ull;//不加ull错误 过不去
//long long ullaOver32 = 18446744073709551615+1ull;//编译错误 过不去
cout <<"ulla32不越界:"<<ulla32<<endl;//18446744073709551615
//cout <<"ullaOver32越界:"<<ullaOver32<<endl;
//64位 unsigned long long 
cout<<"64位编译器 unsigned long long占:"<<sizeof(unsigned long long)<<endl;//8
unsigned long long ulla64 = 18446744073709551615;//
long long ullaOver64 = 18446744073709551615+1ull;//0
cout <<"ulla64不越界:"<<ulla64<<endl;//18446744073709551615
cout <<"ullaOver64越界:"<<ullaOver64<<endl; 

直接gcc test.c不加参数默认为编译器位数,本机为64位;

gcc test.c -m32 按32位编译器编译,反之在64位机上gcc test.c -m64

 

常用数据类型对应字节数
   可用如sizeof(char),sizeof(char*)等得出 

 32位编译器:
 
      char :1个字节
       char*(即指针变量): 4个字节(32位的寻址空间是2^32, 即32个bit,也就是4个字节。同理64位编译器)
       short int : 2个字节
       int:  4个字节
       unsigned int : 4个字节
       float:  4个字节
       double:   8个字节
       long:   4个字节
       long long:  8个字节
       unsigned long:  4个字节
 

  64位编译器:
 
      char :1个字节
       char*(即指针变量): 8个字节
       short int : 2个字节
       int:  4个字节
       unsigned int : 4个字节
       float:  4个字节
       double:   8个字节
       long:   8个字节
       long long:  8个字节
       unsigned long:  8个字节
 

 

参考:http://blog.csdn.net/zhangxinbin5/article/details/7929591 32位和64位系统区别及int字节数       

 

       
http://paddy-w.iteye.com/blog/1403217
 Linux基本数据类型大小——int,char,long int,long long int

 

抱歉!评论已关闭.