C library - va_start() macro



The C stdarg library va_start() macro enables access to the variable arguments following the named argument parmN. It is used to initialize a 'va_list' variable, which is then used to retrieve the additional arguments passed to the function.

The va_start should be called with an instance to a valid va_list (variable list) object ap before any calls to va_arg (variable argument).

This macro is useful for creating a variadic function, allowing us to create a function that can take variable number of arguments. It contains at least one fixed argument followed by an ellipsis (...).

Syntax

Following is the C library syntax of the va_start() macro −

void va_start(va_list ap, parmN)

Parameters

This macro accepts a following parameters −

  • ap − It is a va_list type variable that will be initialize by 'va_start'. This variable is used to traverse the list of arguments.

  • parmN − It is name of the last named parameter in the function definition.

Return Value

This macro does not returns any value.

Example 1

The following is the basic c example that demonstrate the use of va_start().

#include <stdio.h>
#include <stdarg.h>
int sum(int count, ...) {
   va_list args;
   int tot = 0;
   
   // Set the va_list variable with the last fixed argument
   va_start(args, count);
   
   // Retrieve the arguments and calculate the sum
   for (int i = 0; i < count; i++) {
      tot = tot + va_arg(args, int);
   }
   
   // use the va_end to clean va_list variable
   va_end(args);
   
   return tot;
}

int main() {
   // Call the sum, with number of arguments
   printf("Sum of 3, 5, 7, 9: %d\n", sum(4, 3, 5, 7, 9));
   printf("Sum of 1, 2, 3, 4, 5: %d\n", sum(5, 1, 2, 3, 4, 5));
   return 0;
}

Output

Following is the output −

Sum of 3, 5, 7, 9: 24
Sum of 1, 2, 3, 4, 5: 15

Example 2

In this example, we use the va_start() to count the number of passed argument in the user-defined function.

#include <stdio.h>
#include <stdarg.h>
int cnt(int count, ...) {
   va_list args;
   int num_of_arg = 0;

   // Set the va_list variable with the last fixed argument
   va_start(args, count);

   // Retrieve the arguments and count the arguments
   for (int i = 0; i < count; i++) {
      num_of_arg= num_of_arg + 1;
   }    
   // use the va_end to clean va_list variable
   va_end(args);

   return num_of_arg;
}

int main() {
   // Call the sum, with number of arguments
   printf("Number of arguments: %d\n", cnt(4, 3, 5, 7, 9));
   printf("Number of arguments: %d\n", cnt(5, 1, 2, 3, 4, 5));
   return 0;
}

Output

Following is the output −

Number of arguments: 4
Number of arguments: 5

Example 3

Let's see the another example, here we create a function that concatenates a variable number of strings into a single result string.

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

char* concatenate(int count, ...) {
   va_list args;
   int length = 0;
   
   // calculate length
   va_start(args, count);
   for (int i = 0; i < count; i++) {
      length = length + strlen(va_arg(args, char*));
   }
   va_end(args);

   // Allocate memory for the result string
   char *res = (char*)malloc(length + 1);
   if (!res) {
      return NULL;
   }

   // Concatenate the strings
   // Initialize result as an empty string
   res[0] = '\0';
   va_start(args, count);
   for (int i = 0; i < count; i++) {
      strcat(res, va_arg(args, char*));
   }
   va_end(args);

   return res;
}
int main() {
   char *res = concatenate(3, "Hello, ", "tutorialspoint", "India");
   if (res) {
      printf("%s\n", res);
      //free the alocated memory
      free(res);
   }
   return 0;
}

Output

Following is the output −

Hello, tutorialspointIndia
Advertisements