是C“int"的大小吗?2 字节还是 4 字节?
C 中的整数变量是占用 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.