Skip to content

Instantly share code, notes, and snippets.

@sreimers
Created February 7, 2022 09:03
Show Gist options
  • Save sreimers/14eeb8e20dd3f65997be046b4f90bbb2 to your computer and use it in GitHub Desktop.
Save sreimers/14eeb8e20dd3f65997be046b4f90bbb2 to your computer and use it in GitHub Desktop.
#define __format(arg) _Generic((arg), \
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", \
_Bool: "%d", \
float: "%g", \
double: "%g", \
long double: "%Lg", \
_Complex double: "%g + %gi", \
char *: "%s", \
void *: "%p", \
char[sizeof(arg)]: "%s", \
default: "<unknown>" \
)
/* Print an argument regardless of its type (see __format). */
#define __print_arg(arg) printf(__format(arg), arg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment