开发者

What does this kind of formatter mean?

printf ("%#p [ buf  ] (%.2d) : %s \n", buf, strlen (buf), buf);

开发者_StackOverflowI never see %#p (%.2d) before,how does it work?


From http://www.cplusplus.com/reference/clibrary/cstdio/printf/:

Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.

Used with e, E and f, it 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.

Used with g or G the result is the same as with e or E but trailing zeros are not removed.

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 specifies to print an address (i.e. a pointer). The # flag specifies "alternate form", which in this case, probably prepends 0x to the output.


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


Not sure if that is a valid use of the '#' flag:

  • Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
  • Used with e, E and f, it 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.
  • Used with g or G the result 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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜