C Variable arguments
Sometimes, you may encounter a situation where you want a function to have a variable number of arguments, rather than a predefined number of arguments.
The C language provides a solution for this situation, allowing you to define a function that can accept a variable number of arguments according to specific requirements.
The declaration method is:
int func_name(int arg1, ...);
Where, the ellipsis...Represents a variable argument list.
The following example demonstrates the use of this function:
Please note, the functionfunc()The last parameter is written as an ellipsis, i.e., three dots (...), the parameter before the ellipsis isint, which represents the total number of variable arguments to be passed. In order to use this feature, you need to use thestdarg.hheader file, which provides functions and macros for implementing variable arguments. The specific steps are as follows:
- Define a function whose last parameter is an ellipsis, and custom parameters can be set before the ellipsis.
- In the function definition, create ava_listtype variable, this type is defined in the stdarg.h header file.
- UsageintParameters andva_start()Macro to initializeva_listvariable as a parameter list. The macrova_start()is defined in the stdarg.h header file.
- Usageva_arg()macro andva_listvariable to access each item in the argument list.
- Use macrova_end()to clean up the assignedva_listThe variable's memory.
Commonly used macros include:
-
va_start(ap, last_arg): Initializes the variable argument list.apis ava_listA variable of type,last_argis the name of the last fixed parameter (that is, the parameter before the variable argument list). This macro causesapto point to the first parameter in the variable argument list. -
va_arg(ap, type): Obtain the next parameter in the variable argument list.apis ava_listA variable of type,typeis the type of the next parameter. This macro returns a value of typetypethe value, andapPoints to the next argument. -
va_end(ap): Ends access to the variable argument list.apis ava_listvariable of type. This macroapset toNULL。
Now let us follow the above steps to write a function with a variable number of parameters and return their average:
Example
In the above example, the average() function accepts an integer num and any number of integer arguments. Inside the function, a variable va_list of type va_list is used to access the variable argument list. In the loop, each time the va_arg() macro is used to obtain the next integer argument and output it. Finally, at the end of the function, the va_end() macro is used to end the access to the variable argument list.
When the above code is compiled and executed, it produces the following results. It should be noted that the function average()is called twice, and each time the first parameter indicates the total number of variable arguments passed. The ellipsis is used to pass a variable number of arguments.
Average of 2, 3, 4, 5 = 3.500000 Average of 5, 10, 15 = 10.000000other extensions