为什么%p和%x的格式在格式字符串中不同?

时间:2022-09-06 16:32:28

When printing a hexadecimal value (%x) and an address (%p), the format is slightly different. The printed value does not start with 0x in the case of a hexadecimal value:

当打印十六进制值(%x)和一个地址(%p)时,格式略有不同。在十六进制值的情况下,打印的值不以0x开头:

int main()
{
     int x = 0x1234;
     printf("Value of x: %x\n", x);
     printf("Address of x: %p\n", (void*)&x);
}

yields (gcc):

收益率(gcc):

Value of x: 1234
Address of x: 0xffb0fbfc

Why is the 0x forced on you in the case of an address?

为什么在有地址的情况下0x是强加给你的?

I guess it boils down to the standard.

我想这可以归结为标准。

What would be the correct way to print an address without the 0x if i wanted to? The %p is not only a %x with an added 0x right?

如果我想打印一个没有0x的地址,正确的方式是什么?%p不仅仅是添加了0x的%x,对吧?

4 个解决方案

#1


9  

p

p

The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printable characters, in an implementation-defined manner.

论点应该是指向无效的指针。指针的值以实现定义的方式转换为可打印字符序列。

reference

参考

#2


5  

The output format for %p is implementation specific. Not every C implementation is on machines with addresses of the same size as int. There is the intptr_t from <stdint.h>

%p的输出格式是特定于实现的。不是每一个C实现都在具有与int相同大小的地址的机器上,从 中有intptr_t。

#3


2  

The %p is not only a %x with an added 0x right?

%p不仅仅是添加了0x的%x,对吧?

No.. %p expects the argument to be of type (void *) and prints out the address.

没有. .%p期望参数为类型(void *)并打印出地址。

Whereas %x converts an unsigned int to unsigned hexadecimal and prints out the address.

而%x将无符号整型转换为无符号十六进制并打印出地址。

And coming to what %p does is implementation defined but the standard just says that %p expects void* argument else the behavior is undefined.

而对于%p所做的是定义的实现,但是标准只是说%p期望void*参数,否则行为是未定义的。

#4


1  

MSVC does not force the "0x" prefix on me, but you could optionally remove it like this:

MSVC不会对我强制使用“0x”前缀,但您也可以这样删除:

#include <stdio.h>
#include <string.h>

int main(void) {
    int x = 123;
    char hexstr[20];
    sprintf(hexstr,"%p", (void*)&x);
    if (strstr(hexstr,"0x") == hexstr)
        printf ("%s\n", hexstr+2);
    else
        printf ("%s\n", hexstr);
    return 0;
}

#1


9  

p

p

The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printable characters, in an implementation-defined manner.

论点应该是指向无效的指针。指针的值以实现定义的方式转换为可打印字符序列。

reference

参考

#2


5  

The output format for %p is implementation specific. Not every C implementation is on machines with addresses of the same size as int. There is the intptr_t from <stdint.h>

%p的输出格式是特定于实现的。不是每一个C实现都在具有与int相同大小的地址的机器上,从 中有intptr_t。

#3


2  

The %p is not only a %x with an added 0x right?

%p不仅仅是添加了0x的%x,对吧?

No.. %p expects the argument to be of type (void *) and prints out the address.

没有. .%p期望参数为类型(void *)并打印出地址。

Whereas %x converts an unsigned int to unsigned hexadecimal and prints out the address.

而%x将无符号整型转换为无符号十六进制并打印出地址。

And coming to what %p does is implementation defined but the standard just says that %p expects void* argument else the behavior is undefined.

而对于%p所做的是定义的实现,但是标准只是说%p期望void*参数,否则行为是未定义的。

#4


1  

MSVC does not force the "0x" prefix on me, but you could optionally remove it like this:

MSVC不会对我强制使用“0x”前缀,但您也可以这样删除:

#include <stdio.h>
#include <string.h>

int main(void) {
    int x = 123;
    char hexstr[20];
    sprintf(hexstr,"%p", (void*)&x);
    if (strstr(hexstr,"0x") == hexstr)
        printf ("%s\n", hexstr+2);
    else
        printf ("%s\n", hexstr);
    return 0;
}