stdarg.h

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

  • va_list 类型
  • va_start()
  • va_arg():获取当前参数
  • va_end()。

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()
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. int my_printf(int serial, const char *format, ...)
  4. {
  5. va_list va;
  6. // Do my custom work
  7. printf("The serial number is: %d\n", serial);
  8. // Then pass the rest off to vprintf()
  9. va_start(va, format);
  10. int rv = vprintf(format, va);
  11. va_end(va);
  12. return rv;
  13. }
  14. int main(void)
  15. {
  16. int x = 10;
  17. float y = 3.2;
  18. my_printf(3490, "x is %d, y is %f\n", x, y);
  19. }