vastart: This function is utilized for setting up the valist object to reference the initial variables provided to the function.
The va_arg function is utilized for iterating through variable arguments, or simply put, it is beneficial for fetching the subsequent argument.
As the name suggests, va_copy is utilized to generate a duplicate of the arguments provided to the function.
The vaend function is responsible for cleaning up the valist object when the program reaches its conclusion.
Example 1:
Let's consider a program to demonstrate the va_list in the C programming language:
#include <stdio.h>
#include <stdarg.h>
float findMean(int delimiter, ...) {
int sum = 0;
va_list args;
va_start(args, delimiter);
int arg;
int count=0;
while ((arg = va_arg(args, int)) != delimiter) {
sum += arg;
count++;
}
va_end(args);
return sum/count;
}
int main() {
float res = findMean(-1, 10, 10, 10, 10, -1);
printf("Sum: %f\n", res);
return 0;
}
Output:
Sum: [value]
Explanation:
In this instance, the code utilizes va_list to handle variable arguments passed into the function. The script comprises of two functions: the primary function and the findMean function. Within the primary function, there is an invocation to the findMean function, responsible for determining the average of the provided dataset. The call setup consists of two components. The initial component is the delimiter, which signals the completion of the process when entered by the user, producing the output up to that point while the findMean function continues its operations. The second part comprises plain numerical data followed by a delimiter at the conclusion.
The findMean function utilizes a valist object to manage variable arguments denoted by three dots (…). Its purpose is to calculate the total sum of all variables and the count of variables, then determining the mean by dividing the sum by the count. Within this program, the valist and vastart macro are employed, while the vaarg macro is utilized to access the variable arguments.
Example 2:
Let's consider another program to showcase the va_list in the C programming language:
#include<stdio.h>
#include<stdarg.h>
void joinString(const char* delimiter, ...){
va_list args;
va_start(args,delimiter);
const char* arg;
while((arg=va_arg(args,char*))!=NULL){
printf("%s",arg);
}
}
int main(){
joinString(" ","Logic Practice","Welcomes","You"," ");
printf("\n");
joinString(" ","Door","To","Solution");
}
Output:
Sum: [value]
Explanation:
In this illustration, the aforementioned function is employed to concatenate the provided words within the function to construct a sentence during the output process. This particular code segment mirrors the initial program and encompasses a duo of functions, typically featuring one designated as the primary function and the other denoted as the joinString function. Within the primary function, an invocation is initiated towards the joinString function including both the delimiter and the designated words.
Within the joinString function, the utilization of valist enables variable arguments, vastart is utilized to pinpoint the object to the initial word, and vaargs facilitates the retrieval of the specified words within the function. Subsequently, a while loop is implemented to iterate until all words are exhausted for printing. The termination condition is identified as the vaarg being equivalent to the NULL value, signifying the completion of the function's operation, subsequently transitioning control back to the primary function.
Example 3:
Let's consider a program to showcase the va_list in the C programming language:
include<stdio.h>
#include<stdarg.h>
void findMinMax(int count, ...){
va_list args1,args2;
va_start(args1,count);
va_copy(args2,args1);
int i=0;
int mini= 1000;
for(i=0;i<count;i++){
int temp = va_arg(args1,int);
if(mini > temp){
mini= temp;
}
}
i=0;
int maxi = -1;
for(i=0;i<count;i++){
int temp = va_arg(args2,int);
if(maxi < temp){
maxi= temp;
}
}
printf("%d is the minimum \n",mini);
printf("%d is the maximum ",maxi);
va_end(args1);
va_end(args2);
}
int main(){
int n= 5;
findMinMax(n,10,20,30,40,50);
return 0;
}
Output:
Sum: [value]
Explanation:
In this illustration, the code comprises of two functions: the primary function and the findMinMax function. Within the main function, there is a invocation of the findMinMax function, which specifies the count of variables to be passed along with the numerical data. Within the findMinMax function, there are four crucial macros associated with the va_list. This function is employed for determining the minimum and maximum values from the variable arguments.