Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__typeof variables and printf

Tags:

c

gcc

printf

If I'm defining a generic macro, using __typeof__/typeof, is there any way to also pick a printf conversion specifier, in a generic way ?

what I mean is, for e.g:

#define max(a,b) \
 ({ typeof (a) _a = (a); \
    typeof (b) _b = (b); \
    DEBUG(( "%" __PRI_TYPE_PREFIX(a) > "%" __PRI_TYPE_PREFIX(b) "?", _a, _b)) \  <-- hypothetical
    _a > _b ? _a : _b; })

is possible ?

like image 810
vyom Avatar asked Mar 29 '26 21:03

vyom


2 Answers

You can use C11's _Generic capability to do this, e.g.

#define printf_dec_format(x) _Generic((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", \
    char *: "%s", \
    void *: "%p")

#define print(x) printf(printf_dec_format(x), x)

(example taken from: Rob's Programming Blog)

like image 124
Paul R Avatar answered Apr 02 '26 04:04

Paul R


#include <stdio.h>

#define FMT(_pre_, x, _post_) _Generic((x), \
  char: _pre_ "%c" _post_, \
  int: _pre_ "%d" _post_, \
  long: _pre_ "%ld" _post_)

int main()
{
  int x = 42;
  long y = 432144312432321;
  printf(FMT("foo: ", x, "\n"), x);
  printf(FMT("bar: ", y, "\n"), y);
  return 0;
}

The pre and post stuff is a little ugly, but I didn't find a better way yet. We cannot rely on string concatenation in the C preprocessor, because _Generic is evaluated by the compiler, too late.

like image 24
John Zwinck Avatar answered Apr 02 '26 04:04

John Zwinck