Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Macros – va_end()

C Library Macros - va_end()

C Standard Library - <stdarg.h> C Standard Library -

va_end The macro is a macro in the C standard library a macro in the header, used to clean up va_list variable and make it no longer point to any valid memory location. It is used at the end of a variadic function to finish processing the variadic arguments.

C Library Macros void va_end(va_list ap) allows a function with variadic arguments that used the va_start macro to return. If the macro is not called before returning from the function, va_endthe result is undefined.

Declaration

The following is the declaration of the va_end() macro.



void va_end(va_list ap);

Parameters

Return Value

This macro does not return any value.

Usage steps

Example

The following is an example of using va_end a sample program for the macro that defines a variadic function to compute the sum of a variable number of arguments:

Example

#include

#include

// Variadic function that computes the sum of its arguments

int

sum

(

int

count

,

...

)

{

   

int

total

=

0

;

    va_list args

;

   

// Initialize args to access the variadic arguments

   

va_start

(

args

,

count

)

;

   

// Retrieve each variadic argument in turn and add it to total

   

for

(

int

i

=

0

;

i

<

count

;

i

++

)

{

        total

+=

va_arg

(

args

,

int

)

;

   

}

   

// Clean up args

   

va_end

(

args

)

;

   

return

total

;

}

int

main

(

)

{

   

printf

(

"Sum of 1, 2, 3: %d\n"

,

sum

(

3

,

1

,

2

,

3

)

)

;

 

// Output 6

   

printf

(

"Sum of 4, 5, 6, 7: %d\n"

,

sum

(

4

,

4

,

5

,

6

,

7

)

)

;

 

// Output: 22

   

return

0

;

}

Let's compile and run the above program, which will produce the following result:



Sum of 1, 2, 3: 6

Sum of 4, 5, 6, 7: 22

Parsing

Notes

C Standard Library - <stdarg.h> C Standard Library -