这种格式化程序是什么意思?
printf ("%#p [ buf ] (%.2d) : %s \n", buf, strlen (buf), buf);
我以前从未见过%#p (%.2d)
,它是如何工作的?
printf ("%#p [ buf ] (%.2d) : %s \n", buf, strlen (buf), buf);
I never see %#p (%.2d)
before,how does it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 http://www.cplusplus.com/reference/clibrary/cstdio/printf/ :
所以在你的情况下它似乎没有做任何事情,因为
p
用于打印指针地址。我想有些编译器可能会对此有不同的解释,但我找不到任何提及。From http://www.cplusplus.com/reference/clibrary/cstdio/printf/:
So it seems to do nothing in your case, since
p
is used to print a pointer address. I guess some compilers might interpret this differently, but I can't find any mention of it.p
指定打印一个地址(即指针)。#
标志指定“替代形式”,在本例中,可能会在输出前面添加0x
。p
specifies to print an address (i.e. a pointer). The#
flag specifies "alternate form", which in this case, probably prepends0x
to the output.它是格式标识符的标志。
它很可能会在指向的值之前打印出 0x (但我还没有检查TBH)
找到了一个很好的解释 此处
It's a flag for the format identifier.
It will more than likely print out 0x before the pointed value (but I have not checked TBH)
A good explanation is found here
不确定这是否是“#”标志的有效使用:
值前面带有 0、0x 或 0X
分别对于不同的值
比零。
强制写入输出包含
即使没有数字也有小数点
会跟随。默认情况下,如果没有
后面跟着数字,没有小数点
书面。
与 e 或 E 相同,但是
尾随零不会被删除。
它很可能会打印指针的替代格式形式,并将 0x 附加到地址。
Not sure if that is a valid use of the '#' flag:
value is preceeded with 0, 0x or 0X
respectively for values different
than zero.
forces the written output to contain
a decimal point even if no digits
would follow. By default, if no
digits follow, no decimal point is
written.
is the same as with e or E but
trailing zeros are not removed.
It will most likely print an alternately formatted form for the pointer, appending 0x to the address.
在您的情况下(
p
conversion),根据手册页,结果是未定义的。无论如何,%p
和%#p
在我的机器上打印相同的值(看起来像 0x7FFFF000)In your case (
p
conversion) the result is undefined according to the man page. Anyway,%p
and%#p
prints the same value on my machine (looks like 0x7FFFF000)