How to printf a 64-bit integer as hex?

148,631

Solution 1

The warning from your compiler is telling you that your format specifier doesn't match the data type you're passing to it.

Try using %lx or %llx. For more portability, include inttypes.h and use the PRIx64 macro.

For example: printf("val = 0x%" PRIx64 "\n", val); (note that it's string concatenation)

Solution 2

Edit: Use printf("val = 0x%" PRIx64 "\n", val); instead.

Try printf("val = 0x%llx\n", val);. See the printf manpage:

ll (ell-ell). A following integer conversion corresponds to a long long int or unsigned long long int argument, or a following n conversion corresponds to a pointer to a long long int argument.

Edit: Even better is what @M_Oehm wrote: There is a specific macro for that, because unit64_t is not always a unsigned long long: PRIx64 see also this stackoverflow answer

Share:
148,631

Related videos on Youtube

sergej
Author by

sergej

I love C, C++, Linux, Embedded Systems, High-End Audio and Pelmeni

Updated on July 09, 2022

Comments

  • sergej
    sergej almost 2 years

    With the following code I am trying to output the value of a unit64_t variable using printf(). Compiling the code with gcc, returns the following warning:

    warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘uint64_t’ [-Wformat=]

    The code:

    #include <stdio.h>
    #include <stdint.h>
    
    int main ()
    {
        uint64_t val = 0x1234567890abcdef;
        printf("val = 0x%x\n", val);
    
        return 0;
    }
    

    The output:

    val = 0x90abcdef
    

    Expected output:

    val = 0x1234567890abcdef
    

    How can I output a 64bit value as a hexadecimal integer using printf()? The x specifier seems to be wrong in this case.

    • M Oehm
      M Oehm almost 9 years
      See here. basically, use the PRIx64 macro from <inttypes.h>.
    • Daniel
      Daniel about 5 years
      Use printf("val = %#018"PRIx64"\n", val); to print leading zeros. Don't forget to #include <inttypes.h>.
  • David Ljung Madison Stellar
    David Ljung Madison Stellar about 7 years
    I don't believe your edit is correct - it's not "%PRIx64" - it's a string concat with the value defined by the macro PRIx64
  • Superlokkus
    Superlokkus about 7 years
    @DavidLjungMadison You were right, fixed it
  • Top-Master
    Top-Master about 5 years
    also %016llx could be used to ensure output has padding with zero (at the beginning) to reach minimum field width of 16 characters
  • user2023370
    user2023370 over 4 years
    Why does 0000000000000000 not get a 0x at the front?
  • Welgriv
    Welgriv about 4 years
    For the record, PRIx64 is #defined to "ll" "x"
  • tangrs
    tangrs about 4 years
    @Welgriv on which platform and compiler though? There's no guarantee that the macro will be defined to "ll" "x" on all platforms, and that's the whole point of the macro.
  • Welgriv
    Welgriv about 4 years
    Yes, but inttypes.h looks the same in any platform. At least I have the same on Win10 (MINGW), Linux (gcc) and embed environment too (armcc).
  • tangrs
    tangrs about 4 years
    @Welgriv I'm not sure what point you're trying to make. I don't doubt that many popular, modern platforms share the same definition of PRIx64.
  • tangrs
    tangrs about 4 years
    @Welgriv It is certainly a mistake to assume that this is necessarily true on "any platform", though. I'm sure there's a counterexample out there, if one looks hard enough. Whether this is a real problem in practice is a different matter, however.
  • swineone
    swineone about 3 years
    PRIX64 also works, and prints uppercase rather than lowercase letters (A-F).