Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

va_start — initialize access to variable arguments

The va_start macro from <stdarg.h> initializes a va_list for reading arguments passed through an ellipsis. In C23, calling va_start(ap) is sufficient.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdarg.h>
void va_start(va_list ap, ...);

va_start is a macro, not a function. Inside a variadic function, it initializes a va_list before accessing ... arguments; it returns no value.

C23 permits va_start(ap). Additional arguments support older code; only the first is evaluated.

va_arg reads successive arguments. Code must know their count and promoted types; incorrect reads can cause undefined behavior.

Each va_start requires va_end in the same function before returning, also before reinitializing the same ap. Use va_copy to copy traversal state.

Examples

#include <stdarg.h>
#include <stdio.h>

static int sum(unsigned count, ...)
{
    va_list ap;
    va_start(ap, count);
    int total = 0;
    for (unsigned i = 0; i < count; ++i) total += va_arg(ap, int);
    va_end(ap);
    return total;
}

int main(void)
{
    printf("%d\n", sum(3, 10, 20, 30));
    return 0;
}

Result

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.

Sources