Print a 64-bit integer using 32-bit integers
To print a 64-bit unsigned integer in a portable way, ideally you would do as follows:
Where PRIu64
is a macro provided by inttypes.h
, that typically expands to some string literal like llu
. So upon compiling, the string will concatenate to form %llu
, which formats an unsigned long long
value in decimal:
However, if targeting an embedded platform with a stripped down version of libc1, such as newlib-nano, this may silently fail! While the type uint64_t
may exist, and the PRIu64
macro may also exist, if the version of printf
(provided by libc) does not support the %llu
macro, instead of printing 18369614221190020847
, it will just print the string literal %llu
2:
If you cannot change your version of libc, and need to print in decimal, there is no easy way around this.3
However, if you can tolerate printing in hexadecimal rather than decimal, you can seamlessly print the 64-bit unsigned integer as two 32-bit unsigned integers:
Here are some considerations for embedded C standard libraries. ↩︎
May be
llu
,lu
, or potentially evenu
, depending upon your platform. ↩︎If you can switch from newlib-nano to newlib, it may be possible to use the flag
--enable-newlib-io-long-long
. ↩︎