C Library Macro -va_start()
C Standard Library - <stdarg.h>
Description
va_startMacros are the C standard library.<stdarg.h>A macro in a header file, used to initialize ava_listvariable so that the number of variadic arguments can be accessed in a variadic function.
C Library Macrovoid va_start(va_list ap, last_arg)Initializeapvariable, whichva_argandva_endMacros are used together.last_argis the last known fixed parameter passed to the function, i.e., the parameter before the ellipsis.
This macro must be used inva_argandva_endwas called before.
Declaration
Below is the declaration of the va_start() macro.
void va_start(va_list ap, last);
Parameter
va_list ap: This is a type used to access variadic arguments. It will be initialized after the macro call and used in subsequentva_argobtaining arguments in a call.last: This is the last known fixed parameter before the variadic argument list. That is, the last parameter in the function parameter list before the variadic arguments begin.
Return Value
NA
Usage steps
- To define a variadic function, you must have at least one fixed parameter (i.e.,
lastparameters). - Declare a
va_listA variable used to store information about the variadic argument list. - Usage
va_startMacro initializationva_listvariable. - Usage
va_argThe macro retrieves each variable argument in turn. - Usage
va_endMacro cleanupva_listvariable.
Example
The following example demonstrates the use of the va_start() macro.
Example
#include <stdio.h>
#include <stdarg.h>
// Variadic function, calculates the sum of arguments
int sum(int count, ...) {
int total = 0;
va_list args;
// Initialize args to access variadic arguments
va_start(args, count);
// Get each variadic argument one by one and add to total
for (int i = 0; i < count; i++) {
total += va_arg(args, int);
}
// Clean up args
va_end(args);
return total;
}
int main() {
printf("Sum of 1, 2, 3: %d\n", sum(3, 1, 2, 3)); // Output 6
printf("Sum of 4, 5, 6, 7: %d\n", sum(4, 4, 5, 6, 7)); // Output 22
return 0;
}
#include <stdarg.h>
// Variadic function, calculates the sum of arguments
int sum(int count, ...) {
int total = 0;
va_list args;
// Initialize args to access variadic arguments
va_start(args, count);
// Get each variadic argument one by one and add to total
for (int i = 0; i < count; i++) {
total += va_arg(args, int);
}
// Clean up args
va_end(args);
return total;
}
int main() {
printf("Sum of 1, 2, 3: %d\n", sum(3, 1, 2, 3)); // Output 6
printf("Sum of 4, 5, 6, 7: %d\n", sum(4, 4, 5, 6, 7)); // Output 22
return 0;
}
Let us compile and run the above program, which will produce the following result:
Sum of 1, 2, 3: 6 Sum of 4, 5, 6, 7: 22
Parsing
va_list args;: declare ava_listvariable of typeargs, used to store information about the variadic argument list.va_start(args, count);: initializeargsto access fromcountVariable arguments that start afterwards.total += va_arg(args, int);: Useva_argThe macro retrieves the next variadic argument, assuming they are allinttype, and accumulate it intototalin the variable.va_end(args);: clean upargs, so as to release resources and avoid undefined behavior.
Notes
va_startmust withva_list、va_argandva_endUsed together.- Call
va_startAfter, must callva_endto end the processing of variable arguments. - When calling
va_startBefore, you must ensure that the function's parameter list has at least one fixed parameter. lastThe parameter is the last fixed parameter before the variadic argument list, used to determine where to start processing the variadic arguments.