sergej sergej - 6 months ago 74
C Question

How to printf a 64bit integer as hex?

With the following code I am trying to output the value of a

variable using
. 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
? The
specifier seems to be wrong in this case.


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)