stdarg.h
https://www.tutorialspoint.com/c_standard_library/stdarg_h.htm
API
Library Variables
Following is the variable type defined in the header stdarg.h −
No.
Variable & Description
1
va_list
This is a type suitable for holding information needed by the three macros va_start(), va_arg() and va_end().
Library Macros
Following are the macros defined in the header stdarg.h −
No.
Macro & Description
1
void va_start(va_list ap, parmN)
This macro enables access to variadic function arguments.
2
This macro retrieves the next argument in the parameter list of the function with type type.
3
This macro allows to end traversal of the variadic function arguments.
4
void va_copy( va_list dest, va_list src )
This macro makes a copy of the variadic function arguments.
Demo
#include <stdarg.h>
#include <stdio.h>
// 这是一个可变参数函数,它接受任意数量的整数参数
void print_numbers(int count, ...)
{
va_list ap;
va_start(ap, count); // 初始化va_list,设置参数列表的第一个参数
// 使用va_arg宏遍历可变参数列表
for (int i = 0; i < count; i++)
{
int number = va_arg(ap, int); // 获取下一个整数参数
printf("Number %d: %d\n", i + 1, number);
}
va_end(ap); // 清理va_list,释放资源
}
int main()
{
// 调用print_numbers函数,传递一个整数参数列表
print_numbers(5, 1, 2, 3, 4, 5);
return 0;
}
最后更新于
这有帮助吗?