我正在尝试用 C 打印内存地址,这是我正在使用的代码
#include <stdio.h>
int main() {
int v = 10;
printf("Address of the v: %p\n",&v);
return 0;
}
输出应该以0x开头,但是我没有这个东西,它显示了这个
0061FF14
我正在使用 MingW32 gcc 编译器
我将其更改为 64,当我再次输入代码时,输出为 000000A57A7FF67C
当我使用https://www.onlinegdb.com/时输出正常为0x
我的机器是64位,我使用vscode编辑器,如何使地址显示为正常的0x
void *
)的指针。"0x"
,只需将其添加到格式字符串中即可。 printf("Address of the v: 0x%p\n",(void *)&v);
您看到的行为是特定于编译器和系统的。
%p
格式说明符应以实现定义的方式打印地址,通常包含 0x
前缀,但不能保证在所有系统或编译器上都如此。
如果您想确保地址打印时带有
0x
前缀。您可以将地址转换为足够大以容纳指针的无符号整数类型(例如 uintptr_t
标头中的 <stdint.h>
)并使用 %#x
或 %#lx
格式说明符:
#include <stdio.h>
#include <stdint.h>
int main() {
int v = 10;
printf("Address of the v: %#lx\n", (uintptr_t)&v);
return 0;
}