V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
paparika
V2EX  ›  Linux

c 语言,如何实现可变参数函数调用可变参数函数

  •  
  •   paparika · Jul 13, 2018 · 2424 views
    This topic created in 2844 days ago, the information mentioned may be changed or developed.

    比如想实现一个函数 foobar(const char *format, ...){ printf(); }

    只是在 printf 外面包裹一层,这里面怎么实现

    2 replies    2018-07-13 11:14:28 +08:00
    missdeer
        1
    missdeer  
       Jul 13, 2018   ❤️ 1
    paparika
        2
    paparika  
    OP
       Jul 13, 2018
    谢楼上,结贴
    #include <stdarg.h>

    void printf_wrapper(const char *format, ...)
    {
    va_list args;
    va_start(args, format);

    vprintf(format, args);//不可以是 printf!

    va_end(args);
    }

    #define printf_wrapper_macro(format,args...) \
    { \
    printf(format, ## args); \
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2750 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 00:03 · PVG 08:03 · LAX 17:03 · JFK 20:03
    ♥ Do have faith in what you're doing.