【发布时间】:2012-04-17 18:51:09
【问题描述】:
可能重复:
size of int, long, etc
Does the size of an int depend on the compiler and/or processor?
What decides the sizeof an integer?
我正在使用64-bit 机器。
$ uname -m
x86_64
$ file /usr/bin/file
/usr/bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, stripped
$
当我运行以下程序时,我得到了sizeof(int) 为4-bytes。
#include <stdio.h>
int main(void)
{
printf("sizeof(int) = %d bytes\n", (int) sizeof(int));
return 0;
}
如果我正在运行16-、32- 和64- 位机,那么这是否意味着integer 的大小是16-、32- 和64- 位分别?
在我的机器中,我发现WORD_BIT 是32。在64-bit 机器上不应该是64 吗?
$ getconf WORD_BIT
32
$
而且,在上述情况下,sizeof(int) 不应该是 64-bits (8 bytes) 吗?
【问题讨论】:
-
我不是 C 人,但我相信除了
char之外的任何东西的大小都是特定于编译器的,也不一定是处理器上一个字的大小。 -
@minitech: char 也是编译器特定的
-
sizeof(char)根据定义始终为 1。char是 sizeof 返回的隐含单位。 -
说“这是一台 64 位机器”的丑陋之处在于:64 位是什么? 64位数据总线?地址总线?整数寄存器?