简体   繁体   中英

printf with %a does not seem to produce a hexadecimal number

I have been told that "%a" used in C's printf would display hexadecimal format of a number. To test it, I print out the representation of 2^10:

 printf ("%a", pow(2.0,10));

which gives

0x1p+10

I am confused because the exponent part "+10" looks more like a decimal format rather than a hexadecimal format. A hexadecimal format should have been 1pA . Where am I wrong?

It's correct, that format is called hexadecimal for double s.

The man page says:

For a conversion, the double argument is converted to hexadecimal notation (using the letters abcdef ) in the style [-]0xh.hhhp[+-]d [...] the exponent consists of a positive or negative sign followed by a decimal number representing an exponent of 2.

So it's correct that while the mantissa is in hex, the exponent is still decimal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM