Variable Length Argument in C Last Updated : 02 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Variable length argument is a feature that allows a function to receive any number of arguments. There are situations where we want a function to handle variable number of arguments according to requirement. 1) Sum of given numbers. 2) Minimum of given numbers. and many more. Variable number of arguments are represented by three dotes (...) Below is an example, to find minimum of given set of integers. C // C program to demonstrate use of variable // number of arguments. #include <stdarg.h> #include <stdio.h> // this function returns minimum of integer // numbers passed. First argument is count // of numbers. int min(int arg_count, ...) { int i; int min, a; // va_list is a type to hold information about // variable arguments va_list ap; // va_start must be called before accessing // variable argument list va_start(ap, arg_count); // Now arguments can be accessed one by one // using va_arg macro. Initialize min as first // argument in list min = va_arg(ap, int); // traverse rest of the arguments to find out minimum for (i = 2; i <= arg_count; i++) if ((a = va_arg(ap, int)) < min) min = a; // va_end should be executed before the function // returns whenever va_start has been previously // used in that function va_end(ap); return min; } // Driver code int main() { int count = 5; printf("Minimum value is %d", min(count, 12, 67, 6, 7, 100)); return 0; } Here we use macros to implement the functionality of variable arguments. Use va_list type variable in the function definition.int a_function(int x, ...) { va_list a_list; va_start( a_list, x ); }Use int parameter and va_start macro to initialize the va_list variable to an argument list. The macro va_start is defined in stdarg.h header file.Use va_arg macro and va_list variable to access each item in argument list.macro va_end to clean up the memory assigned to va_list variable. Another Example : C // C program to demonstrate working of // variable arguments to find average // of multiple numbers. #include <stdarg.h> #include <stdio.h> int average(int num, ...) { va_list valist; int sum = 0, i; va_start(valist, num); for (i = 0; i < num; i++) sum += va_arg(valist, int); va_end(valist); return sum / num; } // Driver code int main() { printf("Average of {3, 4} = %d\n", average(2, 3, 4)); printf("Average of {5, 10, 15} = %d\n", average(3, 5, 10, 15)); return 0; } Output: Average of {3, 4} = 3 Average of {5, 10, 15} = 10 Comment More infoAdvertise with us Next Article Variable Length Argument in C S Shivani Baghel 1 Follow Improve Article Tags : Misc C Language C-Functions Practice Tags : Misc Similar Reads How to Count Variable Numbers of Arguments in C? C supports variable numbers of arguments. But there is no language provided way for finding out total number of arguments passed. User has to handle this in one of the following ways: 1) By passing first argument as count of arguments. 2) By passing last argument as NULL (or 0). 3) Using some printf 2 min read Local Variable in C In C language, a variable declared within a function or a block of code is called a local variable. Local variables are frequently used to temporarily store data in a defined scope where they can be accessed and manipulated. They are stored in the memory stack, Once the function or block of code in 3 min read Command Line Arguments in C The most important function of C is the main() function. It is mostly defined with a return type of int and without parameters.int main() { ... }We can also give command-line arguments in C. Command-line arguments are the values given after the name of the program in the command-line shell of Operat 4 min read Length of Array in C The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements.In C language, we don't have any pre-defin 3 min read C Program to print environment variables The C standard says following about main function in C. The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: int main(void) { /* ... */ } or with two parameters (referre 1 min read Like