printf ptr:可以消除开头的0x吗? [英] printf ptr: can the leading 0x be eliminated?

查看:92
本文介绍了printf ptr:可以消除开头的0x吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux printf将%p参数呈现为带有前导0x的十六进制数字.有没有办法使它不打印0x?(需要同时在32位和64位上工作.)

The Linux printf renders %p arguments as hex digits with a leading 0x. Is there a way to make it not print the 0x? (Needs to work on both 32 and 64 bit.)

推荐答案

您可以将 uintptr_t 的格式说明符用于< inttypes.h> :

You can use the format specifier for uintptr_t from <inttypes.h>:

#include <inttypes.h>
[...]
printf("%"PRIxPTR"\n", (uintptr_t) p);

对于 uintptr_t 类型,它的作用类似于%x ,它是一种整数类型,能够从/向任何指针类型进行往返转换.

This works like %x for the uintptr_t type, which is an integer type capable of roundtrip conversion from/to any pointer type.

这篇关于printf ptr:可以消除开头的0x吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆