stdarg.h in C

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:

stdarg.h in C Picture 1

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:

stdarg.h in C Picture 2

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:

stdarg.h in C Picture 3

According to Tutorialspoint

Last lesson: Function signal () in C

Next lesson: stddef.h in C

4.3 ★ | 4 Vote

May be interested

  • Check out the 'buffalo' Pokémon in Pokémon GoCheck out the 'buffalo' Pokémon in Pokémon Go
    each type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
  • Sitting home can also locate Pokemon around, do you believe it?Sitting home can also locate Pokemon around, do you believe it?
    the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
  • 5 undeniable benefits when playing Pokemon Go5 undeniable benefits when playing Pokemon Go
    get to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
  • Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!Want to earn the fastest Pokécoins in Pokémon Go?  So don't miss this article!
    pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.
  • Pokémon systems when fighting in Pokémon GoPokémon systems when fighting in Pokémon Go
    each pokémon system in pokémon go has different strengths, along with a specific weakness. this type of pokémon will have the power to attack the other pokémon, but can defeat the other pokémon. if you know the characteristics of each type, it will be easier to choose which pokémon to battle.
  • The terms you need to know when playing Pokémon GoThe terms you need to know when playing Pokémon Go
    pokémon go is the most prominent name in recent days. this game of capturing and training virtual animals has created a relatively new way of playing, as players have to constantly move to catch pokémon. during the process of joining pokémon go, you will encounter and use a lot of important terms. so what do they mean?
  • How to play Pokemon GO on Windows computersHow to play Pokemon GO on Windows computers
    recently, pokemon go has become a popular game, attracting thousands of gamers around the world. in previous posts network administrator has guided you on how to play pokemon go on android devices and ios devices. in the article below, network administrator will guide you how to play pokemon go on windows computers using bluestacks emulator software.
  • How to become a good Pokémon Go hunter?How to become a good Pokémon Go hunter?
    with a new way of playing, pokémon go has created a new phenomenon in the game village around the world. players will have to go around and find pokémon for themselves. so how to quickly catch pokémon and in large numbers?
  • Things to know to upgrade Pokémon in Pokémon GoThings to know to upgrade Pokémon in Pokémon Go
    the whirlwind pokémon go has spread its power right on the first day of its release. everyone rushed to the street to find a pokémon for themselves. therefore, in order to become a pokémon hunter, you need to master the basics of participating in pokémon go battlefield.
  • List of medals received in Pokemon GoList of medals received in Pokemon Go
    in addition to getting items when leveling up, gamers who join pokemon go also have the opportunity to receive their proud achievements.