C 标准库 stdarg.h详解

stdarg.h定义于函数的可变参数相关的一些方法。

va_copy():it makes a copy of your va_list variable in the exact same state. va_copy() can be useful if you need to scan ahead through the arguments but need to also remember your current place.

接受可变函数作为参数的一些方法。

  • vprintf()
  • vfprintf()
  • vsprintf()
  • vsnprintf()
#include <stdio.h>
#include <stdarg.h>

int my_printf(int serial, const char *format, ...)
{
    va_list va;

    // Do my custom work
    printf("The serial number is: %d\n", serial);

    // Then pass the rest off to vprintf()
    va_start(va, format);
    int rv = vprintf(format, va);
    va_end(va);

    return rv;
}

int main(void)
{
    int x = 10;
    float y = 3.2;

    my_printf(3490, "x is %d, y is %f\n", x, y);
}

教程来源于Github,感谢大佬的无私奉献,致敬!

技术教程推荐

软件测试52讲 -〔茹炳晟〕

程序员进阶攻略 -〔胡峰〕

算法面试通关40讲 -〔覃超〕

深入浅出计算机组成原理 -〔徐文浩〕

后端技术面试 38 讲 -〔李智慧〕

性能工程高手课 -〔庄振运〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

Vue 3 企业级项目实战课 -〔杨文坚〕

结构思考力 · 透过结构看思考 -〔李忠秋〕