Using C11 type generic macros, it is possible to construct the format string at compile time, e.g.:
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#define PRI3(B,X,A) _Generic((X), \
unsigned char: B"%hhu"A, \
unsigned short: B"%hu"A, \
unsigned int: B"%u"A, \
unsigned long: B"%lu"A, \
unsigned long long: B"%llu"A, \
signed char: B"%hhd"A, \
short: B"%hd"A, \
int: B"%d"A, \
long: B"%ld"A, \
long long: B"%lld"A)
#define PRI(X) PRI3("",(X),"")
#define PRIFMT(B,X,A) PRI3(B,(X),A),(X)
int main () {
signed char sc = SCHAR_MIN;
unsigned char uc = UCHAR_MAX;
short ss = SHRT_MIN;
unsigned short us = USHRT_MAX;
int si = INT_MIN;
unsigned ui = UINT_MAX;
long sl = LONG_MIN;
unsigned long ul = ULONG_MAX;
long long sll = LLONG_MIN;
unsigned long long ull = ULLONG_MAX;
size_t z = SIZE_MAX;
intmax_t sj = INTMAX_MIN;
uintmax_t uj = UINTMAX_MAX;
(void) printf(PRIFMT("signed char : ", sc, "\n"));
(void) printf(PRIFMT("unsigned char : ", uc, "\n"));
(void) printf(PRIFMT("short : ", ss, "\n"));
(void) printf(PRIFMT("unsigned short : ", us, "\n"));
(void) printf(PRIFMT("int : ", si, "\n"));
(void) printf(PRIFMT("unsigned int : ", ui, "\n"));
(void) printf(PRIFMT("long : ", sl, "\n"));
(void) printf(PRIFMT("unsigned long : ", ul, "\n"));
(void) printf(PRIFMT("long long : ", sll, "\n"));
(void) printf(PRIFMT("unsigned long long: ", ull, "\n"));
(void) printf(PRIFMT("size_t : ", z, "\n"));
(void) printf(PRIFMT("intmax_t : ", sj, "\n"));
(void) printf(PRIFMT("uintmax_t : ", uj, "\n"));
}
There is a potential problem, though: if there are types distinct from those listed (i.e., other than signed
and unsigned
versions of char
, short
, int
, long
, and long long
), this does not work for those types. It is also not possible to add types such as size_t
and intmax_t
to the type generic macro "just in case", because it will cause an error if they are typedef
d from one of the already listed types. I've left the default
case unspecified so that the macro generates a compile-time error when no matching type is found.
However, as seen in the example program, size_t
and intmax_t
work just fine on platforms where they are the same as one of the listed types (e.g., same as long
). Similarly there is no issue if, e.g., long
and long long
, or long
and int
, are the same type. But a safer version might be to just cast to intmax_t
or uintmax_t
according to signedness (as seen in other answers), and make a type generic macro with only those options…
A cosmetic problem is that the type generic macro expands with parentheses around the string literal, preventing concatenation with adjacent string literals in the usual way. This prevents things like:
(void) printf("var = " PRI(var) "\n", var); // does not work!
Hence the PRIFMT
macro with prefix and suffix included for the common case of printing a single variable:
(void) printf(PRIFMT("var = ", var, "\n"));
(Note that it would be simple to expand this macro with the non-integer types supported by printf
, e.g., double
, char *
…)
std::cout << ino ;
. The appropriate<<
operator overload for the argument type will be used - no format specifiers necessary. If there is no compelling reason to use C and this is a common need, C++ may be useful even if you are not intending to write OO code. For the most part your C code need not change at all, except where C++ provides a better mousetrap - operator overloading in this case being one of them.