How to print hexadecimal double in C? How to print hexadecimal double in C? unix unix

How to print hexadecimal double in C?


That's an integer, and a long one. Don't use double to store such a value, that's for floating-point.

Just use:

unsigned long long temp = 0xffffffffffffull;

You have 12 hexadecimal digits, so your number needs at least 12 * 4 = 48 bits. Most platforms should have an unsigned long long of 64 bits, which should be fine.

If your compiler is supported enough to support C99, you can do:

#include <stdint.h>

and then use the uint_least64_t type as suggested in a comment. In Linux I guess you're using GCC so you should be fine, you might need to specify that you intend your code to be compiled as C99 (with -std=c99).

To print it, use:

printf("temp=%llx\n", temp);

Also note that the value itself is not hexadecimal, but you can print it as hexadecimal. THe value is just a value, the base matters only when converting to/from text, i.e. an external representation of the number. Internally on a binary computer, the number is stored in binary.


While you store the value in a double, there's no sane way to get hexadecimal output.

Your code passes a double to printf() and repeatedly tries to format that as a plain int; that is not going to work well. If you used GCC with warnings enabled, you should have got warnings about mismatches between format string and value (and if you use GCC without warnings enabled, repent your evil/lazy ways and turn the compiler warnings on — use -Wall at least, and probably -Wextra too, and fix any resulting warnings).

The value is 12 F's, so it is a long long value (or unsigned long long); store it and treat it as such:

int main(){    unsigned long long a = 0xFFFFFFFFFFFF;    printf("%llx\n", a);    printf("%lld\n", a);    printf("%llX\n", a);    printf("0x%llX\n", a);    return 0;}


In another context, his question is valid. Say you want to print the output of the pow() function in hexadecimal - I just encountered one now.

printf( "pow(a, b) is : %f\n", pow(a, b) );  //Will give decimal value printf( "pow(a, b) is : %X\n", pow(a, b) );  //Not allowed 

So do

unsigned long long int temp; temp = pow(a, b); printf( "pow(a, b) is : %X\n", temp);       //Will give hexadecimal value