C Library Macro -va_arg()
C Standard Library - <stdarg.h>
va_argThe macro is a C standard library<stdarg.h>A macro in the header file, used to sequentially retrieve the value of each variable argument in a variadic function. It is used afterva_startMacro initializedva_listthe variable, to access the next argument in the variable argument list.
Declaration
Below is the declaration of the va_arg() macro.
type va_arg(va_list ap, type);
Parameter
va_list ap: oneva_listvariable of type, which has already been passed throughva_startThe macro initializes, pointing to the current position in the variable argument list.type: The type of the next argument to be retrieved. It must be a type that can be clearly identified.
Return Value
Returns the next argument in the current variable argument list, with type type.
Usage steps
- In a variadic function, use
va_startMacro initializationva_listvariable. - Usage
va_argthe macro to sequentially obtain each variable argument. - Usage
va_endMacro cleanupva_listvariable.
Example
The following is a use ofva_argAn example program for the macro, which defines a variadic function to calculate the sum of a variable number of arguments:
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 variable argument list.va_start(args, count);: initializeargsto access fromcountthe variable arguments that start aftertotal += va_arg(args, int);: Useva_argThe macro retrieves the next variable argument, here assuming they are allinttype, and accumulate it intototalin the variable.va_end(args);: clean upargs, in order to release resources and avoid undefined behavior.
Notes
va_argThe macro should beva_startThen use it, and atva_endused beforehand.typeThe argument must be a definite type, and it should match the actual type of the variable argument.- If calling
va_argWhen it exceeds the number of variable arguments, the behavior is undefined. - Usage
va_endcleanupva_listThe variable is necessary to avoid potential resource leaks or undefined behavior.