开发者

printf precision

开发者 https://www.devze.com 2023-03-20 16:31 出处:网络
I am writing a program in C and I have several printf statements for debugging. Is there a way to change the precision for a HEX output on开发者_JAVA技巧 printf? Example. I have 0xFFF but I want it to

I am writing a program in C and I have several printf statements for debugging. Is there a way to change the precision for a HEX output on开发者_JAVA技巧 printf? Example. I have 0xFFF but I want it to print out 0x0FFF.


Say printf("%04X", x);.

The 0 means "pad with zeros", the 4 means "at least four characters wide".

For integers, one doesn't use the term "precision" (because integers are precise), but rather "field width" or something like that. Precision is the number of digits in scientific notation when printing floats.


You can use the precision field for printf when printing in hex.

For instance:

int i = 0xff;
printf ("i is 0x%.4X\n", i);
printf ("i is  %#.4X\n", i);

Will both print: 0x00FF

0

精彩评论

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