int为什么是4个字节? 如题,int为什么是4个字节?别告诉我本来就是四个字节这种话,我刚被人问的时候也这么想的…有人知道深层原因么? int 类型长度 [解决办法] int是最基本的类型,一般和CPU的字宽一致。 [解决办法] 为了和CPU的字宽一致,提高处理速度。。。 [解决办法] 操作系统16位的时候,int 2字节,操作系统32位的时候,int 4字节,由于32位系统之前占主流地位,实际现在就算是64位系统,出于兼容性考虑,int也是4字节的 [解决办法] 看编译器规定 还有2个字节8字节呢 [解决办法] The C Programming language这本书,里面有一句话是这样的:Each compiler is free to choose appropriate sizes for its own hardware, subject only to the restriction that shorts and ints are at least 16bits, longs are at least 32bits, and short is no longer than int, which is no longer than long.意思大致是编译器可以根据自身硬件来选择合适的大小,但是需要满足约束:short和int型至少为16位,long型至少为32位,并且short型长度不能超过int型,而int型不能超过long型。这即是说各个类型的变量长度是由编译器来决定的,而当前主流的编译器中一般是32位机器和64位机器中int型都是4个字节(例如,GCC)。下面列举在GCC编译器下32位机器和64位机器各个类型变量所占字节数: C类型 32 64 char 1 1 short int 2 2 int 4 4 long int 4 8 long long int 8 8
The C Programming language这本书,里面有一句话是这样的:Each compiler is free to choose appropriate sizes for its own hardware, subject only to the restriction that shorts and ints are at least 16bits, longs are at least 32bits, and short is no longer than int, which is no longer than long.意思大致是编译器可以根据自身硬件来选择合适的大小,但是需要满足约束:short和int型至少为16位,long型至少为32位,并且short型长度不能超过int型,而int型不能超过long型。这即是说各个类型的变量长度是由编译器来决定的,而当前主流的编译器中一般是32位机器和64位机器中int型都是4个字节(例如,GCC)。下面列举在GCC编译器下32位机器和64位机器各个类型变量所占字节数: C类型 32 64 char 1 1 short int 2 2 int 4 4 long int 4 8 long long int 8 8 char* 4 8 float 4 4 double 8 8 需要说明一下的是指针类型存储的是所指向变量的地址,所以32位机器只需要32bit,而64位机器需要64bit。