header file. It is useful when we do not know the number of arguments that are passed to the user-defined function. We can easily handle such a problem with the help of va_list.">

va_list in C

va_list is special data type in C language. It is less known to the world but it more useful for programmers. It is present in <stdarg.h> header file. It is useful when we do not know the number of arguments that are passed to the user-defined function. We can easily handle such a problem with the help of va_list.

Now, we will discuss the macros related to the va_list. The <stdarg.h> header will provide four main macros, that are va_start, va_arg, va_copy, va_end which are useful while using the va_list datatype.

va_start: It is used to initialize the va_list object so that it will point to the first variables that are passed to the function.

va_arg: This is used to iterate through the variable arguments or simply we can say that it is useful for retrieving the next argument.

va_copy: The name implies, that it is used to create a copy of the arguments passed to the function.

va_end: It will clear the va_list object at the end of the program.

Example 1:

Let's take a program for illustrating the va_list in C:

Output:

va_list in C

Explanation:

In this example, the program uses va_list for variable arguments passed to the function. This program contains two functions that are main function and the findMean function. In the main function, a call is made to the findMean function which is used to caculate the mean of the given data. The call definition contains two parts first part is the delimiter that mean if the user enters the delimiter then the process will be completed, and gives the output until then it will do its work in the findMean function. The other part is simple numerical data and a delimiter at the end.

The findMean function has va_list object for handling the variable arguments that are represented by three dots (…). This function is used to find the sum of all the variables are the count of all variables then dividing the sum by count gives the required mean. In this program, the va_list and va_start macro are used and va_arg macro is used for accessing the variable arguments.

Example 2:

Let's take another program to demonstrate the va_list in C:

Output:

va_list in C

Explanation:

In this example, the above function is used to join the words given to the function to make a sentence while printing. The above program is the same as the program one. It contains two functions, as usually one is the main function and the other is joinString function. In the main function, a call is made to the joinString function containing the delimiter and the words given to the function. In joinString function, we use the va_list for variable arguments, va_start for pointing the object to the first word, and va_args for retrieving the words given to the function. After that, a while loop is used to iterate until there is no word present to print. Here the ending condition is va_arg should be equal to the NULL value then function work will be completed and control will go to the main function.

Example 3:

Let's take a program to demonstrate the va_list in C:

Output:

va_list in C

Explanation:

In this example, the program contains two functions that are main function and findMinMax function. Here, the main function contains the call to the findMinMax function, which contains the total number of the variables to pass and the numerical data. In findMinMax function, we can see all four important macros which are related to the va_list. The function used for calculating the minimum and maximum from the variable arguments.