totn C Functions

C Language: va_end function
(Fetch Argument from Variable Argument List)

In the C Programming Language, the va_end function ends the processing of ap (the variable argument list).

Syntax

The syntax for the va_end function in the C Language is:

void va_end(va_list ap);

Parameters or Arguments

ap
A variable argument list.

Returns

The va_end function does not return anything.

Required Header

In the C Language, the required header for the va_end function is:

#include <stdarg.h>

Applies To

In the C Language, the va_end function can be used in the following versions:

  • ANSI/ISO 9899-1990

va_end Example

/* Example using va_end by TechOnTheNet.com */

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

int add(int n, ...)
{
    /* Define temporary variables */
    va_list list;
    int total;

    /* Initialize total */
    total = 0;

    /* Set where the variable length part of the argument list ends */
    va_start(list, n);

    /* Loop through each argument adding the int values */
    for (int i=0; i < n; i++)
        total = total + va_arg(list, int);

    /* Clean up */
    va_end(list);

    /* Return the calculated total */
    return total;
}

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    int value1, value2, value3;
    int result;

    value1 = 2;
    value2 = 3;
    value3 = 4;

    /* Call the add function  */
    result = add(4, value1, value2, value3);

    /* Display the results of the additon */
    printf("The sum of %d, %d and %d is %d\n", value1, value2, value3, result);

    return 0;
}

When compiled and run, this application will output:

The sum of 2, 3 and 4 is 9

See Also

Other C functions that are noteworthy when dealing with the va_end function: