On Thu, Feb 01, 2024 at 02:13:11PM +0100, Jakub Jelinek wrote: > Or hwint.h could define PRIusize_t etc. macros and some corresponding type > to be used in casts, something like > #if SIZE_MAX == LONG_LONG_MAX > #define PRIusize_t HOST_LONG_LONG_FORMAT "u" > #define fmt_size_t unsigned long long > #elif SIZE_MAX == LONG_MAX > #define PRIusize_t HOST_LONG_FORMAT "u" > #define fmt_size_t unsigned long > #elif SIZE_MAX == INT_MAX > #define PRIusize_t "u" > #define fmt_size_t unsigned int > #else > #error "Unsupported size_t" > #endif > > and then use > "... %" PRIusize_t " ... ", ... (fmt_size_t) (some_size_t_expr), ... > > Or at configure time figure out which exact type size_t is say (unsigned > long long vs. unsigned long vs. unsigned int) using templates and > use that info to pick the right format modifier depending on that, then > the casts aren't needed.
Note, the former is probably better and might need some fallback, because on some platforms size_t is some weird type like __uint20 or similar. So maybe just start with smallest to largest, #if SIZE_MAX <= INT_MAX ... #elif SIZE_MAX <= LONG_MAX ... #else ... #endif. Jakub