C __变量类型和printf

C __变量类型和printf,c,gcc,printf,C,Gcc,Printf,如果我使用\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu 我的意思是,例如: #define max(a,b) \ ({ typeof (a) _a = (a); \ typeof (b) _b = (b); \ DEBUG(( "%" __PRI_TYPE_PREFIX(a) > "%" __PRI

如果我使用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

我的意思是,例如:

#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; })
定义最大值(a,b)\ ({typeof(a)_a=(a)\ 类型(b)_b=(b)\ 调试(“%”类型前缀(a)>“%”类型前缀(b)“?”,\U a,\U b))\\U b?\U a:\U b;})

可能吗?

您可以使用C11的
\u Generic
功能来执行此操作,例如

#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)
(示例取自:)

#包括
#定义FMT(_pre_uuuuuu,x,_post_uuuuu)_Generic((x)\
字符:_pre_uuu“%c”\u post\uuu\
int:_pre_uuuuuuuuuuuuud”\u post\uuuu\
长:_pre_uuu“%ld”\u post_uuu)
int main()
{
int x=42;
长y=432144312432321;
printf(FMT(“foo:”,x,“\n”),x);
printf(FMT(“条:”,y,“\n”),y);
返回0;
}

前期和后期的东西有点难看,但我还没有找到更好的方法。我们不能依赖C预处理器中的字符串连接,因为编译器对
\u Generic
进行计算太晚了。

例如,使用C11
\u Generic
(必须完成才能进行完整检查):

或者使用
\u内置类型\u兼容\u p
gcc扩展:

#define max(a,b) \
 ({ typeof (a) _a = (a); \
    typeof (b) _b = (b); \
    if (__builtin_types_compatible_p(typeof(int), _a) && __builtin_types_compatible_p(typeof(int), _a))  DEBUG_INT(_a, _b);  \
    else if  (__builtin_types_compatible_p(typeof(double), _a) && __builtin_types_compatible_p(typeof(double), _a))  DEBUG_DOUBLE(_a, _b); \
    else FAIL(_a, _b);
    _a > _b ? _a : _b; })

从博客链接中,我可以看到只有
clang
支持此功能!:(,到目前为止,我使用的是
gcc-4.6.3
@vyom gcc还支持
\u Generic
,但您需要更新的版本。
#define DEBUG_INT(a, b)     DEBUG("%d %d?", (a), (b))
#define DEBUG_DOUBLE(a, b)  DEBUG("%f %f?", (a), (b))
#define FAIL(a, b)          assert(0)

#define max(a,b) \
 ({ typeof (a) _a = (a); \
    typeof (b) _b = (b); \
    _Generic((_a), int: DEBUG_INT, double: DEBUG_DOUBLE, default: FAIL))(_a, _b); \
    _a > _b ? _a : _b; })
#define max(a,b) \
 ({ typeof (a) _a = (a); \
    typeof (b) _b = (b); \
    if (__builtin_types_compatible_p(typeof(int), _a) && __builtin_types_compatible_p(typeof(int), _a))  DEBUG_INT(_a, _b);  \
    else if  (__builtin_types_compatible_p(typeof(double), _a) && __builtin_types_compatible_p(typeof(double), _a))  DEBUG_DOUBLE(_a, _b); \
    else FAIL(_a, _b);
    _a > _b ? _a : _b; })