stdarg.h in C

The file header named stdarg.h in Library C defines a type of va_list variable and 3 macros that can be used to retrieve parameters in a function when the number of parameters is unknown (eg the number of parameters is can change).

stdarg.h in C

The file header named stdarg.h in Library C defines a type of va_list variable and 3 macros that can be used to retrieve parameters in a function when the number of parameters is unknown (eg the number of parameters is can change).

The variables are defined in stdarg.h

The following is the type of variable defined in stdarg.h:

va_list :   This is a suitable type to store the information needed by the three macros, va_start (), va_arg () and va_end ().

Macros are defined in stdarg.h

The table below lists some macros defined in stdarg.h in Library C:

MacroVoid description and_start (va_list ap, last_arg)

This macro initializes the variable to be used with two macros, va_arg and va_end. The last_arg parameter, the last known fixed parameter, is being passed to the function, for example: the parameter before the ellipsis

type va_arg (va_list ap, type)

This macro accepts the next parameter in the parameter list of the function with the type

void va_end (va_list ap)

This macro allows a function with a variable number of parameters to use the macro as va_start to return. If va_end is not called before returning from that function, the result is not defined

1. The va_start () macro in C

Macro void va_start (va_list ap, last_arg) in Library C initializes the variable to be used with two macros, va_arg and va_end. The last_arg parameter, the last known fixed parameter, is being passed to the function, for example: the parameter before the ellipsis.

This macro must be called before using va_arg and va_end.

Declaring macro va_start () in C

Below is the declaration for va_start () macro.

 void va_start ( va_list ap , last_arg ); 

Parameters

ap - This is the object of va_list and it holds the information needed to obtain additional parameters with va_arg .

last_arg - This is the last known fixed parameter being passed to the function.

Returns the value

This macro does not return any values.

For example

The following C program illustrates the use of va_start () macros.

 #include #include int sum ( int , .); int main ( void ) { printf ( "Tong cua 15, 25 va 35 = %dn" , sum ( 3 , 15 , 25 , 35 ) ); printf ( "Tong cua 5, 10, 15 va 20 = %dn" , sum ( 4 , 5 , 10 , 15 , 25 ) ); return 0 ; } int sum ( int num_args , .) { int val = 0 ; va_list ap ; int i ; va_start ( ap , num_args ); for ( i = 0 ; i < num_args ; i ++) { val += va_arg ( ap , int ); } va_end ( ap ); return val ; } 

Compiling and running the above C program will result:

Picture 1 of stdarg.h in C

2. Va_arg () macro in C

Va_arg type macro (va_list ap, type) in Library C acquires the next parameter in the parameter list of functions with type. This macro does not decide whether or not the parameter is taken as the parameter passed to the function.

Declaring va_arg () Macro in C

Below is the declaration for va_arg () macro.

 type va_arg ( va_list ap , type ) 

Parameters

ap - This is the object of the va_list type with information about additional parameters and acquisition status. This object should be initialized by an initial call to and_start before the first call to va_arg.

type - This is the type name. This type of name is used as the type of expression, which this macro extends to.

Returns the value

This macro returns the additional parameter as an expression of the type.

For example

The following C program illustrates the use of va_arg () macro.

 #include #include int sum ( int , .); int main () { printf ( "Tong cua 25 va 52 = %dn" , sum ( 2 , 25 , 52 ) ); return 0 ; } int sum ( int num_args , .) { int val = 0 ; va_list ap ; int i ; va_start ( ap , num_args ); for ( i = 0 ; i < num_args ; i ++) { val += va_arg ( ap , int ); } va_end ( ap ); return val ; } 

Compiling and running the above C program will result:

Picture 2 of stdarg.h in C

3. Macro va_end () in C

Macro void va_end (va_list ap) in Library C allows a function with a variable number of parameters to use the macro as va_start to return. If va_end is not called before returning from that function, the result is not defined.

Declaring Macro va_end () in C

Below is the declaration for va_end () macro.

 void va_end ( va_list ap ) 

Parameters

ap - This is the va_list object that was initialized by va_start in the same function.

Returns the value

This macro does not return any values.

For example

The following C program illustrates the use of va_end () macro.

 #include #include int mul ( int , .); int main () { printf ( "11 * 15 = %dn" , mul ( 2 , 11 , 15 ) ); return 0 ; } int mul ( int num_args , .) { int val = 1 ; va_list ap ; int i ; va_start ( ap , num_args ); for ( i = 0 ; i < num_args ; i ++) { val *= va_arg ( ap , int ); } va_end ( ap ); return val ; } 

Compiling and running the above C program will result:

Picture 3 of stdarg.h in C

According to Tutorialspoint

Last lesson: Function signal () in C

Next lesson: stddef.h in C

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile