What does a hash sign '#' do in printf()?

十年热恋 提交于 2020-05-27 05:42:44

问题


int x = 0xff;
printf("%#x",x);

Output: 0xff

printf("%x",x);

Ouput: ff

Why is there a difference in output? What does # specifically do?


回答1:


The standard says:

7.21.6 - 2

The result is converted to an ‘‘alternative form’’. ... For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to it.

It does other interesting stuff (especially for floats) but I have rarely seen it used - I honestly admit I had to look it up to remember.



来源:https://stackoverflow.com/questions/11922876/what-does-a-hash-sign-do-in-printf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!