Programming

Variable number of arguments in a function

Every C programmer has used printf statements. And every C programmer knows that printf is nothing but a function already defined in the C library (usr/lib). But there are very few programmers which could figure out that printf is different from other functions.Still Wondering how? That’s because  printf function can accept variable number of arguments while others can’t. So let’s explore the what and why factor of this concept…

C allows to write functions with a fixed argument followed by a variable number of arguments.

Ex – int sum (int total, )

Total is the variable which is mandatory but after that any number of arguments can follow. … represents that there can be any number of arguments.  are called ellipses. The first argument (total in this case) is called first argument or named argument whereas the variable number of arguments are called unnamed arguments.

Note : All the arguments should be of the same datatype.

C provides a header file stdarg.h (standard arguments header file) which contains four macros which implement variable number of arguments in C.

These are:

  1. va_list

This macro deals with declaring a pointer (or nominating a pointer) which would be assigned an address in a later stage.

  1. va_start

This macro assigns a particular address to the pointer declared in the previous stage. The address assigned is nothing but the staring address of the list of variable arguments. It takes two arguments these are the pointer declared in the first step and the named variable.

  1. va_arg

This macro fetches values from the list of arguments. It increments the base address (obtained in previous step) appropriately to fetch all the arguments. It takes two arguments these are the pointer declared in the first step and the datatype of the values to be fetched.

  1. va_end

This macro just cleans up the mess. It should be used before the return statement of the function.

Examples:

1.

#include<stdio.h>    // standard input output header file. compile time error if not included.

#include<stdarg.h>   // if not included it results in compile time error. Remember (non-inclusion of appropriate header files leads to compile time error).

int sumt (int , . . .) ; // function declaration.

int main ()

{

int result;

result = sumt(5,3,2,4,5,6);

pritnf(” result is %d”, result);

result = sumt(4,2,3,5,7);

pritnf(” result is %d”, result);

result = sumt(8,3,2,4,5,6,7,5,4);

pritnf(” result is %d”, result);

return 0;

}

int sumt(int total, . . .)

{

va_list p; // macro va_list to declare (or nominate) a pointer variable.

int i, sum =0;

va_start (p, total); // calling macro va_start to store the address of first unnamed variable.

for (i=0 ; i< total ; i++)

{

sum = sum + va_arg(p , int);// macro va_arg returns the next int value being pointed by the pointer. pointer value automatically increments in every iteration.

}

return sum;

}

 

2.

 

#include<stdio.h>    //Remember(non incluseon of apropriate header files leads to compile time error).
#include<stdarg.h>  
int max (int , . . .) ; 
int main ()
{
	int maximum;
	maximum = max(5,3,2,4,5,6);
	pritnf(" result is %d", maximum);
	rmaximum = max(4,2,3,5,7);
	pritnf(" result is %d", maximum);
	maximum = max(8,3,2,4,5,6,7,5,4);
	pritnf(" result is %d", maximum);
	return 0;
}
int max(int total, . . .)
{
	va_list p; // macro va_list to declare (or nominate) a pointer variable.
	int i,num, max =0;
	va_start (p, total); // calling macro va_start to store the adress of first unnamed variable.
	for (i=0 ; i< total ; i++)
	{
		num = var_arg(p , int);// macro va_arg returns the next int value being pointed by the pointer. pointer value automaticlly increments in every iteration.
		if(num > max)
		max = num;
	}
    return max;
}

Though this concept (variable arguments) is not so commonly used but it’s important for the printf function.

The printf function uses this concept. A simplified implementation of printf using varargs will be implemented in the next article.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s