How to printf a 64-bit integer as hex?
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
Related videos on Youtube
sergej
I love C, C++, Linux, Embedded Systems, High-End Audio and Pelmeni
Updated on July 09, 2022Comments
-
sergej almost 2 years
With the following code I am trying to output the value of a
unit64_t
variable usingprintf()
. 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()
? Thex
specifier seems to be wrong in this case.-
M Oehm almost 9 years
-
Daniel about 5 yearsUse
printf("val = %#018"PRIx64"\n", val);
to print leading zeros. Don't forget to#include <inttypes.h>
.
-
-
David Ljung Madison Stellar about 7 yearsI 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 about 7 years@DavidLjungMadison You were right, fixed it
-
Top-Master about 5 yearsalso
%016llx
could be used to ensure output haspadding
with zero (at the beginning) to reachminimum field width
of16
characters -
user2023370 over 4 yearsWhy does
0000000000000000
not get a0x
at the front? -
Welgriv about 4 yearsFor the record,
PRIx64
is#define
d to"ll" "x"
-
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 about 4 yearsYes, 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 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 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 about 3 years
PRIX64
also works, and prints uppercase rather than lowercase letters (A-F).