c语言中%d和%p printf格式字符串指令的区别? [英] Difference between %d and %p printf format string directives in c language?

查看:19
本文介绍了c语言中%d和%p printf格式字符串指令的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想详细解释一下%d%p类型用于打印指针的区别.

I want to get a detailed explanation on the difference between using %d and %p type for printing pointer.

还有为什么 %p 返回十六进制?%d%p 返回不同值的情况是什么?数据类型仅代表用户想要输出的方式还是与内存位置有关?

Also Why does %p return hexadecimal? What are the cases when %d and %p return different values? Does datatype only represent the way the user wants the output or it has something to do with the memory locations too?

推荐答案

要使程序定义明确,格式说明符必须与参数的类型匹配.因此,您可以使用 %p 而不是 %d 来打印指针.(后者可能恰好适用于某些架构,但在技术上是未定义行为.)

For the program to be well-defined, the format specifier must match the type of the argument. Therefore you can use %p but not %d to print out pointers. (The latter might happen to work on some architectures but is technically undefined behaviour.)

您不能自由交换 %d%p 的主要原因是 ints 和指针不必具有大小相同.

The primary reason you can't freely interchange %d and %p is that ints and pointers don't have to have the same size.

打印出指针的格式是特定于体系结构的(指针可以有不同的大小或实际上不同的结构).然而,以十六进制转录内存地址是很常见的,所以这是 %p 通常做的.

The format in which pointers are printed out is architecture-specific (pointers can have different size or indeed different structure). It is, however, common to transcribe memory addresses in hexadecimal, so this is what %p usually does.

这篇关于c语言中%d和%p printf格式字符串指令的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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