The Functions with Variable Number of Arguments

danielstiv999 Programing, Technology ,

We have often used the functions type printf (), scanf (),…
These functions are unique, they accept a variable number of parameters!
Can we also create such functions?
Presentation of a library of the language C “stdarg. h “.
Explanation with examples and exercises.

Functions with variable number of arguments
The C language allows you to define functions whose number of arguments is not fixed and can vary from one call to another. We have often used the following functions:
-printf ()
-scanf ()
These functions are unique: They accept a variable number of parameters!

/* a parameter */
printf ( “Hello! n”);

/* 2 Parameters */
printf ( “Cube of% ld is: n”, value);

/* 4 Parameters */
printf ( “% ld *% ld =%ldn”, I, value, product);

Can we also create such functions?

Declaration and syntax
The declaration is made in the following way:

#include <stdarg.h>
type fonction(type1 arg1, type2 arg2, …)
{
}</stdarg.h>

Call to function:

function (arg1, arg2);

Rules to be respected

Rule 1: If the prototype contains p formal parameters, then the call to function must be done with at least p parameters.
Example:
The following statement is considered:

function type (type1 arg1, type2 arg2, Type3 Arg3,…);

When calling this function we must provide at least 3 arguments:

function (Arg1, arg2, arg3)//appel correct
function (Arg1, arg2)//appel incorrect
function (Arg1, arg2, Arg3, arg4)//appel correct

Rule 2: A function with a variable number of parameters must have at least one fixed parameter.
Example:

int sum (…); Incorrect statement
int sum (int a,…); Correct statement

Rule 3: Scoring… (Mandatory at the end of the parameter list) specifies that the function has a variable number of parameters. This variable list of parameters must always appear in the last position among the formal parameters.
Example:

void error (int n, char * msg,…);

This statement says that the error () function is defined in such a way that calls must provide at least two arguments, one of type int and one of type char *, but they can provide additional arguments.
Sample call:

Error (3, “correct call”);

Access to arguments
To access the arguments after the last fixed argument, you must use some
Functions (or rather macros) of the stdarg. h file

This library contains all the functions needed to access the arguments. It defines a type to be used to process variable lists of parameters,
Va_list array containing the argument information as well as 3 macros to retrieve the value of the parameters.

void Va_start (Va_list ap, last);
Makes AP point to the first variable argument provided to the function.

Type Va_arg (va_list ap, type);
Returns the first variable argument and points AP to the next argument.
Type is the type of the argument that is going to be read and Va_arg generates an expression of this void Va_end (Va_list ap);
returns to normal before return & #224; the calling function.

Call Order
Va_start must be applied before va_arg or va_end.
Va_end must be called after Va_arg has read all the arguments, otherwise the program behavior will be unpredictable.
Progress in the argument list the first call to Va_arg actually returns the first argument in the list
Each subsequent call to Va_arg allows you to retrieve an argument.
Values returned:
-Va_start and Va_end do not return a value.
-Va_arg Returns the current argument of the list (the one on which AP points).

Check out some great web development blog ideas.

Remarks
Char, unsigned char, or float types are forbidden with va_arg.
Parameters passing rules:

For integer-type parameters

-Char, shorts are converted to int.
-long remains in long format.

For real-type parameters

-they are converted to double

You May Also Like..

HTML – What Does It Stand For? Usage and Meaning

HTML The HTML is a computer language used on the internet. This language is used to create web pages. The […]

Matshita UJ-857 Units Flawed Zebra Silenced by Apple

Over the last three days, zebra has been fighting to get the world’s MacBook Pro optical drive users a better […]

ImgBurn – The Ultimate Image Burner

  ImgBurn carries on from where DVD Decrypter left off! (Well, for the burning part anyway!) ImgBurn can write most […]

Leave a Reply

Your email address will not be published. Required fields are marked *