C 中的 Integer 变量是占用 2 个字节还是 4 个字节?它取决于哪些因素?
Does an Integer variable in C occupy 2 bytes or 4 bytes? What are the factors that it depends on?
大多数教科书都说整数变量占用 2 个字节.但是当我运行一个打印整数数组的连续地址的程序时,它显示的差异是 4.
Most of the textbooks say integer variables occupy 2 bytes. But when I run a program printing the successive addresses of an array of integers it shows the difference of 4.
推荐答案我知道它等于 sizeof(int).int 的大小实际上取决于编译器.在过去,当处理器是 16 位时,int 是 2 个字节.如今,在 32 位和 64 位系统上它通常是 4 个字节.
I know it's equal to sizeof(int). The size of an int is really compiler dependent. Back in the day, when processors were 16 bit, an int was 2 bytes. Nowadays, it's most often 4 bytes on a 32-bit as well as 64-bit systems.
不过,对于执行程序的特定系统,使用 sizeof(int) 是获取整数大小的最佳方法.
Still, using sizeof(int) is the best way to get the size of an integer for the specific system the program is executed on.
修正了在大多数 64 位系统上 int 是 8 个字节的错误声明.例如,在 64 位 GCC 上是 4 个字节.
Fixed wrong statement that int is 8 bytes on most 64-bit systems. For example, it is 4 bytes on 64-bit GCC.
更多推荐
C“int"的大小是多少?2字节还是4字节?
发布评论