Petite macro C11 écrite à l'instant (et utilisant la macro MAP référencée précédemment - ou autre implémentation de cette macro):
#define PRINTF_FORMAT(x) _Generic(((void)0,x), \
char: "%c", \
signed char: "%hhd", \
unsigned char: "%hhu", \
signed short: "%hd", \
unsigned short: "%hu", \
signed int: "%d", \
unsigned int: "%u", \
long int: "%ld", \
unsigned long int: "%lu", \
long long int: "%lld", \
unsigned long long int: "%llu", \
float: "%f", \
double: "%f", \
long double: "%Lf", \
const char *: "%s", \
const void *: "%p", \
char *: "%s", \
void *: "%p")
#define PRINT_ARG(x) printf(PRINTF_FORMAT(x), x);
#define PRINT(...) do { MAP(PRINT_ARG, __VA_ARGS__) } while (0)
permettant de faire:
const int x = 142;
PRINT("Ceci est un test. x=", x, " et le flottant vaut ",17.42,"\n");
et affichant bien:
Ceci est un test. x=142 et le flottant vaut 17.420000
(Marche avec gcc et clang)