Va Start In C Programming

The macros are invoked in the program prior to invoking the vaarg and vaend within the function.

Syntax for using va_start function

The definition of the va-start macro in your code:

Example

void va_start( va_list ap, last_arg);

Return Type in va_start function

void: Because the function does not provide any output, the return type specified when defining the va_start function is void.

Parameters Passed in va_start function

  • ap - This parameter refers to the variable list. It is an object that is specifically created for the valist. This object is used for storing the information that the user requires to find the additional arguments using the vaarg function.
  • lastarg - This is the last argument entered in the vastart function. It is a fixed-length argument to be passed in it just before the ellipsis.
  • Header file:

Given that this macro is specified within the stdarg library, it is imperative to incorporate the corresponding header file into your program.

Example

#include<stdarg.h>

Implementation of va_start function in C Program:

Example

#include <stdio.h>

#include <stdarg.h>

//let us define a function that will be used to perform addition

//here we have not defined the total parameters that can be passed in the function

int addition(int n, ...)

{

    // initialize a temporary variable that can be used

    // to store the parameters passed in the functions

    // so that we can add them

    va_list numtoadd;

    int totalsum;



    // Initialize a variable to store

    // the sum of all the parameters in the function.

    totalsum = 0;



    // Use the va_start() function

    // define the variable part of the function and

    // then define the end of the argument lists

    va_start(numtoadd, n);



    // use the loop to add each argument

    // and add all the arguments in the totalsum variable

    for (int i=0; i < n; i++)

        totalsum = totalsum + va_arg(numtoadd, int);



    // now once you have used the list

    // clear the memory

    va_end(numtoadd);



    // now the addition is performed and stored in totalsum

    // return the totalsum

    return totalsum;

}



int main(int argc, const char * argv[])

{

    // Define variables to store the value required for addition

    int val1, val2, val3;

    int res;



    val1 = 3;

    val2 = 4;

    val3 = 5;



    /* Calling the addition function  */

    res = addition(3, val1, val2, val3);



    // Print the answers after performing the addition 

    printf("The sum of %d, %d and %d is %d\n", val1, val2, val3, res);



    return 0;

}

Output:

Output

[Program Output]

Let's explore another instance of displaying the result of multiplying a series of numbers.

Example

#include <stdio.h>

#include <stdarg.h>

//let us define a function that will be used to perform multiplication

//here we have not defined the total parameters that can be passed in the function

int product(int n, ...)

{

    // initialize a temporary variable that can be used

    // to store the parameters passed in the functions

    // that we have to multiply

    va_list numtomul;

    int totalproduct;



    // Initialize a variable to store

    // the product of all the parameters in the function.

    totalproduct = 1;



    // Use the va_start() function

    // define the variable part of the function and

    // then define the end of the argument lists

    va_start(numtomul, n);



    // use the loop to add each argument

    // and add all the arguments in the totalsum variable

    for (int i=0; i < n; i++)

        totalproduct = totalproduct * va_arg(numtomul, int);



    // now once you have used the list

    // clear the memory

    va_end(numtomul);



    // now the multiplication is performed and stored in totalproduct

    // return the totalsum

    return totalproduct;

}



int main(int argc, const char * argv[])

{

    // Define variables to store the value required for performing multiplication

    int val1, val2, val3;

    int res;



    val1 = 3;

    val2 = 4;

    val3 = 5;



    /* Calling the product function  */

    res = product(3, val1, val2, val3);



    // Print the answers after computing product

    printf("The product of %d, %d and %d is %d\n", val1, val2, val3, res);



    return 0;

}

Output:

Output

[Program Output]

Conclusion: Prior to incorporating the vaarg function into your program, it is crucial to first integrate the vastart function within your code and finalize it with the va_end function.

Input Required

This code uses input(). Please provide values below: