Is there a way to fix the format specifier warnings for stdint types?

The problem is that on one platform (windows, mvsc2015) it uint64_t

is defined as unsigned long long

, and on another (ubuntu, clang) it is unsigned long

, and there is a code that looks like sprintf(buffer, "%#llx", u64key);


source to share

3 answers

The solution is to use C99 format macros, in particular PRIu64

for uint64_t


#include <inttypes.h>
sprintf(buffer, "%#" PRIu64 "\n", u64key);




Pascal's solution is the most direct and most idiomatic for a given particular type, but for writing is an alternative for printing arbitrary integer types whose definitions you don't know, just cast to intmax_t

or uintmax_t

, then using j

(for example, %jd

or %ju

). However, this may not work for most / all versions of the MSVC standard library as they are lagging behind standards compliance.



You can use preprocessor directives to determine how the datatype is determined and compile another sprintf () with a different string.



All Articles