YouTip LogoYouTip

C Variable Arguments

# C Variable Arguments Sometimes, you may encounter a situation where you want a function to take a variable number of arguments, rather than a predefined number. The C language provides a solution for this situation, allowing you to define a function that can accept a variable number of arguments depending on specific needs. The declaration is: ```c int func_name(int arg1, ...); where the ellipsis `...` represents the variable argument list. The following example demonstrates the use of such a function: ```c int func(int, ... ){ . . . } int main(){ func(2, 2, 3); func(3, 2, 3, 4); } Please note that the last argument of the function **func()** is written as an ellipsis, i.e., three dots (**...**). The parameter before the ellipsis is **int**, representing the total number of variable arguments to be passed. To use this feature, you need to use the **stdarg.h** header file, which provides the functions and macros that implement variable argument functionality. The specific steps are as follows: * Define a function with the last parameter as an ellipsis. You can set custom parameters before the ellipsis. * Create a variable of type **va_list** in the function definition. This type is defined in the stdarg.h header file. * Use an **int** parameter and the **va_start()** macro to initialize the **va_list** variable as an argument list. The macro **va_start()** is defined in the stdarg.h header file. * Use the **va_arg()** macro and the **va_list** variable to access each item in the argument list. * Use the macro **va_end()** to clean up the memory allocated for the **va_list** variable. The commonly used macros are: * `va_start(ap, last_arg)`: Initializes the variable argument list. `ap` is a variable of type `va_list`, and `last_arg` is the name of the last fixed parameter (i.e., the parameter before the variable argument list). This macro sets `ap` to point to the first argument in the variable argument list. * `va_arg(ap, type)`: Gets the next argument from the variable argument list. `ap` is a variable of type `va_list`, and `type` is the type of the next argument. This macro returns a value of type `type` and advances `ap` to point to the next argument. * `va_end(ap)`: Ends access to the variable argument list. `ap` is a variable of type `va_list`. This macro sets `ap` to `NULL`. Now let's follow the steps above to write a function with a variable number of arguments that returns their average: ## Example ```c #include #include double average(int num,...){ va_list valist; double sum = 0.0; int i; /* Initialize valist for num number of arguments */ va_start(valist, num); /* Access all the arguments assigned to valist */ for(i = 0; i < num; i++){ sum += va_arg(valist, int); } /* Clean memory reserved for valist */ va_end(valist); return sum/num; } int main(){ printf("Average of 2, 3, 4, 5 = %fn", average(4, 2,3,4,5)); printf("Average of 5, 10, 15 = %fn", average(3, 5,10,15)); } In the example above, 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, the va_arg() macro is used each time to get the next integer argument and accumulate it. Finally, when the function ends, the va_end() macro is used to end 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 argument represents the total number of variable arguments passed. The ellipsis is used to pass a variable number of arguments. ```c Average of 2, 3, 4, 5 = 3.500000 Average of 5, 10, 15 = 10.000000
← Php Constant ArraysPhp Spaceship Operator β†’