60

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.

jww
  • 97,681
  • 90
  • 411
  • 885
sergej
  • 17,147
  • 6
  • 52
  • 89
  • 3
    See [here](http://stackoverflow.com/questions/9225567/how-to-print-a-int64-t-type-in-c/9225648#9225648). basically, use the `PRIx64` macro from ``. – M Oehm Aug 20 '15 at 07:53
  • 2
    Use `printf("val = %#018"PRIx64"\n", val);` to print leading zeros. Don't forget to `#include `. – Daniel May 12 '19 at 14:19

2 Answers2

89

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)

tangrs
  • 9,709
  • 1
  • 38
  • 53
  • 5
    also `%016llx` could be used to ensure output has `padding` with zero (at the beginning) to reach `minimum field width` of `16` characters – Top-Master Feb 23 '19 at 07:36
  • 1
    Why does `0000000000000000` not get a `0x` at the front? – user2023370 Feb 06 '20 at 22:05
  • 3
    For the record, `PRIx64` is `#define`d to `"ll" "x"` – Welgriv Apr 21 '20 at 11:59
  • 1
    @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. – tangrs Apr 21 '20 at 12:23
  • 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). – Welgriv Apr 21 '20 at 14:34
  • @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 Apr 23 '20 at 08:25
  • @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. – tangrs Apr 23 '20 at 08:35
  • 1
    `PRIX64` also works, and prints uppercase rather than lowercase letters (A-F). – swineone Apr 29 '21 at 01:42
13
#include <inttypes.h>

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

Aubin
  • 14,617
  • 9
  • 61
  • 84
Superlokkus
  • 4,731
  • 1
  • 25
  • 57