打印结合 _Generic 和 variadic_functions [英] print combine _Generic and variadic_functions

查看:38
本文介绍了打印结合 _Generic 和 variadic_functions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读_Generic 结合可变参数函数?

http://www.robertgamble.net/2012/01/c11-generic-selections.html

但我想让打印就像 python 接收多个和未知类型的参数

but I want to make print just like python receive multi and unknown type parameters

print("a", 1) //printf("%s %d", "a", 1)

这是我的代码

//compiler gcc
#define COUNT(...) ({register int i;int c=1;for(i=0;i<=(strlen(#__VA_ARGS__));i++){if (#__VA_ARGS__[i]==','){c++;}};c;})


#define _print(object) printf( _Generic((object),  \
        _Bool: "bool%d",                  unsigned char: "%hhu",          \
         char: "%c",                     signed char: "%hhd",            \
    short int: "%d",         unsigned short int:"%d",     \
          int: "%d",                     unsigned int: "%u",           \
     long int: "%lu",           unsigned long int: "%lu",      \
long long int: "%llu", unsigned long long int: "%llu", \
        float: "%f",                         double: "%f",                 \
  long double: "%Lf",                   char *: "%s",        \
       void *: "%p",                int *: "%p",         \
      default: "<unknow object at %p>") , object)
void pyprint(int LEN, ...){
    va_list ap;
    va_start(ap, LEN);int i;
    for (int i = 0; i < LEN; i++)
    {
        _print(va_arg(ap, int));
    } }
#define print(...) pyprint(COUNT(__VA_ARGS__), __VA_ARGS__)
//print('A', 10, -10);  ->6510-10
//but print only receive int object because va_arg(ap, int)

推荐答案

坏消息是 COUNT 宏在参数之一包含逗号时将无法工作.典型的例子是 fun(1,2)Hello, Bob!".

The bad news is that COUNT macro will not work if one of arguments has a comma in it. Typical examples would be fun(1,2) or "Hello, Bob!".

我建议使用宏来计算参数,但它限制了支持的最大参数数.

I suggest using a macro to count arguments however it limits maximal number of arguments that are supported.

宏通过在扩展的 VA_ARGS 列表之后添加一个连续整数列表来工作.因此,特定位置的参数将包含许多传递给宏的参数.

The macro works by adding a list of consecutive integers after expanded VA_ARGS list. As result the argument on a specific position will contain a number of arguments passed to the macro.

#define print(...) print_(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0);
#define print_(a0, a1, a2, a3, a4, a5, a6, a7, CNT, ...) \
do {                              \
    if (CNT > 0) _print(a0); \
    if (CNT > 1) _print(a1); \
    if (CNT > 2) _print(a2); \
    if (CNT > 3) _print(a3); \
    if (CNT > 4) _print(a4); \
    if (CNT > 5) _print(a5); \
    if (CNT > 6) _print(a6); \
    if (CNT > 7) _print(a7); \
    puts("");                \
} while (0)

该解决方案最多可以处理 8 个参数,但可以轻松解除此限制.用法:

The solution can handle up to 8 arguments but this limitation can be easily lifted. Usage:

int main() {
    print("a");
    print(3, " / ", 2, " = ", 1.5);
    print('A', 10, -10);
    char a = 'A';
    print(a, 10, -10);
}

印刷品

a
3 / 2 = 1.500000
6510-10
A10-10

'A' 打印为整数的问题 不能轻易解决,因为C 标准要求字符文字为int 类型.参见`字符常量

The problem of printing 'A' as an integer cannot be easily solved because C standard requires character literals to be of type int. See `character constant

这篇关于打印结合 _Generic 和 variadic_functions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆