为什么我的机器是 64 位的,但变量的地址长度只有 6 位十六进制代码?
Why is the length of address of variable only 6 digits in hex code although my machine is 64 bit?
当我尝试以十六进制格式将 C 中的变量地址打印到我的标准输出时,令我惊讶的是,我得到了一个 6 位数的答案。因为,我在 64 位机器上工作,我希望有一个 16 位长地址,因为 2^{64} = 16^{16}
但地址只有 6 位长。
参考代码:
#include<stdio.h>
int square();
int x;
int main(){
scanf("%d",&x);
printf("%d\n",square());
printf("\naddress of x is %x",&x);
return 0;
}
int square(){
return x*x;
}
输出是:
address of x is 407970
首先printf("\naddress of x is %x",&x);
是不正确的。 %x
期望 unsigned int
.
打印地址的正确格式说明符是 %p
(more here):
printf("\naddress of x is %p",&x);
//To be more precise cast to void*, see @Gerhardh's comment below
printf("\naddress of x is %p", (void*)&x);
Why is the length of address of variable only 6 digits in hex code although my machine is 64 bit?
unsigned long x = 10;
printf("%lu", x); //prints 10
x
在上例中为 10。这是否意味着 x
现在是 8 位宽?不。同样有一个从 0 开始的地址范围,地址不必大到总是表示为完整的 64 位。在您的情况下,您可以假设它是 0x00407970
。
旁注: 如果 x
的值很大,以下将有未定义的行为 。输入 123123
看看会发生什么
int square(){
return x*x;
}
当我尝试以十六进制格式将 C 中的变量地址打印到我的标准输出时,令我惊讶的是,我得到了一个 6 位数的答案。因为,我在 64 位机器上工作,我希望有一个 16 位长地址,因为 2^{64} = 16^{16}
但地址只有 6 位长。
参考代码:
#include<stdio.h>
int square();
int x;
int main(){
scanf("%d",&x);
printf("%d\n",square());
printf("\naddress of x is %x",&x);
return 0;
}
int square(){
return x*x;
}
输出是:
address of x is 407970
首先printf("\naddress of x is %x",&x);
是不正确的。 %x
期望 unsigned int
.
打印地址的正确格式说明符是 %p
(more here):
printf("\naddress of x is %p",&x);
//To be more precise cast to void*, see @Gerhardh's comment below
printf("\naddress of x is %p", (void*)&x);
Why is the length of address of variable only 6 digits in hex code although my machine is 64 bit?
unsigned long x = 10;
printf("%lu", x); //prints 10
x
在上例中为 10。这是否意味着 x
现在是 8 位宽?不。同样有一个从 0 开始的地址范围,地址不必大到总是表示为完整的 64 位。在您的情况下,您可以假设它是 0x00407970
。
旁注: 如果 x
的值很大,以下将有未定义的行为 。输入 123123
看看会发生什么
int square(){
return x*x;
}