chexprintfoctalconversion-specifier

What are the fractional octal and hexadecimal conversion specifier in C?


I am aware that when using the printf function I can express the octal value of an integer with %o and its hexadecimal value with %x.
How can I represent a floating-point value as octal or hexadecimal, though?
When trying to use the same specifiers, Xcode 13.x predictably throws a warning telling me I am trying to use an integer specifier with a floating-point value.

How can I work around this?
Thank you


Solution

  • You can format a float or double in hexadecimal using %a or %A (to use lowercase or uppercase letters, respectively). There is no conversion specifier for octal for floating-point.

    This formats the value. If you want to view the representation of the floating-point object, you should access its bytes using an unsigned char *, and then you can format those bytes using the o, x, or X conversion specifiers.