[c] How to printf a 64-bit integer as hex?

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.

This question is related to c gcc 64-bit

The answer is


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)


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


Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to gcc

Can't compile C program on a Mac after upgrade to Mojave Compiling an application for use in highly radioactive environments Make Error 127 when running trying to compile code How to Install gcc 5.3 with yum on CentOS 7.2? How does one set up the Visual Studio Code compiler/debugger to GCC? How do I set up CLion to compile and run? CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found How to printf a 64-bit integer as hex? Differences between arm64 and aarch64 Fatal error: iostream: No such file or directory in compiling C program using GCC

Examples related to 64-bit

Android Studio: /dev/kvm device permission denied How to printf a 64-bit integer as hex? Unable to install Android Studio in Ubuntu I cannot start SQL Server browser Class not registered Error Excel VBA Code: Compile Error in x64 Version ('PtrSafe' attribute required) MSOnline can't be imported on PowerShell (Connect-MsolService error) How to know installed Oracle Client is 32 bit or 64 bit? The application was unable to start correctly (0xc000007b) How to specify 64 bit integers in c