C library - va_arg() macro



The C stdarg library va_arg() macro is used in functions that accept a variable number of arguments and retrieve the next argument in the va_list (variable argument list) initialized by va_start.

Before using va_arg, the object ap of type va_list should be initialized by 'va_start(ap, last_arg)' or 'va_copy(ap, src_ap)' if the argument is copied from another va_list. No calls to va_end should appear in between.

This macro is useful for variadic function, it retrieves a value of a specified type from the location pointed to by arg_ptr, and increments arg_ptr to point to the next argument on the list.

Syntax

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

type va_arg(va_list ap, type)

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.

  • type − It represents the type of argument we want to retrieve. It specifies data type of the next argument in the list.

Return Value

This macro returns the next additional argument as an expression of type type.

Example 1: Sum of number

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

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

int Sum_of_num(int n, ...) {
   int Sum = 0;
   va_list ptr;
   va_start(ptr, n);
   for (int i = 0; i < n; i++){
      Sum += va_arg(ptr, int);
   }        
   va_end(ptr);
   return Sum;
}
int main() {
   printf("1 + 2 is %d\n", Sum_of_num(2, 1, 2));
   printf("1 + 2 + 3 is %d\n", Sum_of_num(3, 1, 2, 3));
   printf("1 + 2 + 3 + 4 is %d\n", Sum_of_num(4, 1, 2, 3, 4));
   return 0;
}

Output

Following is the output −

1 + 2 is 3
1 + 2 + 3 is 6
1 + 2 + 3 + 4 is 10

Example 2: Finding the largest number

In this example, we use the va_arg() to find the largest number from passed argument.

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

int Largest_num(int n, ...) {
   va_list ap;
   va_start(ap, n);
   int max = va_arg(ap, int);
   for (int i = 0; i < n - 1; i++) {
      int temp = va_arg(ap, int);
      max = temp > max ? temp : max;
   }
   va_end(ap);
   return max;
}

int main() {
   printf("%d\n", Largest_num(2, 1, 2));
   printf("%d\n", Largest_num(3, 1, 2, 3));
   printf("%d\n", Largest_num(4, 6, 2, 3, 4));
   return 0;
}

Output

Following is the output −

2
3
6

Example 3

Let's see the another example, create a variadic function that retrieves and prints each argument passed to it.

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

void print_numbers(int count, ...) {
   va_list args;

   // Initialize the va_list variable
   va_start(args, count);

   // Retrieve and display each argument
   for (int i = 0; i < count; i++) {
      int num = va_arg(args, int);
      printf("Argument %d: %d\n", i + 1, num);
   }
   // Clean the va_list
   va_end(args);
}
int main() {
   printf("Displaying 3 numbers:\n");
   print_numbers(3, 10, 20, 30);

   printf("\nDisplaying 5 numbers:\n");
   print_numbers(5, 1, 2, 3, 4, 5);

   return 0;
}

Output

Following is the output −

Displaying 3 numbers:
Argument 1: 10
Argument 2: 20
Argument 3: 30

Displaying 5 numbers:
Argument 1: 1
Argument 2: 2
Argument 3: 3
Argument 4: 4
Argument 5: 5
Advertisements