CKTIS / C
<stdarg.h> — variadic arguments
The <stdarg.h> header provides va_list and macros for reading unnamed arguments of functions with ... and copying traversal state.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <stdarg.h>
va_list ap, copy;
va_start(ap);
value = va_arg(ap, type);
va_copy(copy, ap);
va_end(copy);
va_end(ap);va_start initializes a va_list; C23 permits va_start(ap) without the last named parameter. va_arg(ap, type) retrieves the next argument. The calling contract, not the header, determines the argument count.
va_copy creates independent traversal state; assignment is not a portable substitute. Each initialization requires va_end in the same function.
Account for the default argument promotions: float promotes to double, and types subject to integer promotion promote to int or unsigned int. In C23, an argument of type signed _BitInt(8) that is not a bit-field is exempt from integer promotion. Retrieve it with va_arg(ap, signed _BitInt(8)), not va_arg(ap, int). A mismatched type outside the standard's exceptions, or reading past the end of the argument list, causes undefined behavior.
Examples
#include <stdarg.h>
#include <stdio.h>
static void sum_twice(int count, ...)
{
va_list ap, copy;
va_start(ap, count);
va_copy(copy, ap);
int first = 0, second = 0;
for (int i = 0; i < count; ++i) {
first += va_arg(ap, int);
second += va_arg(copy, int);
}
va_end(copy);
va_end(ap);
printf("%d %d\n", first, second);
}
int main(void)
{
sum_twice(3, 10, 20, 30);
return 0;
}
Result
60 60
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.