codescracker


c

C Variable Arguments



« Previous Tutorial Next Tutorial »


In C, you can specify a function that has a variable number of arguments (parameters). The most common example is printf(). You must end the declaration of the function's arguments or parameters using the three periods or dots to tell the compiler that an unknown number of arguments will be passed to that function.

Declare Variable Arguments in C

Here is the general form to declare variable length arguments in C language:

type function_name(type var_name, ...);

Here type is any valid data type, function_name is any identifier specify the name of the function. In bracket, the type is again any valid data type, and then var_name is an identifier specify the name of variable. You can also declare variable length arguments like this:

type function_name(type var_name1, type var_name2, ...);

Here is an example to declare variable length arguments in C:

int func(int a, ...);

Here the above prototype specifies that func() will have at least one integer argument or parameter and then an unknown number (including 0) of parameters or arguments can have. Now let's look at this prototype.

int func(int a, int b, ...);

Now, this prototype specifies that func() will have at least two integer arguments or parameters and then an unknown number (including 0) of parameters or arguments can have. The above prototype simply tells the compiler that func() must receive at least two parameters or arguments. Now let's look at one more prototype.

int func(int x, int y, int z, ...);

Here the above prototype tells the compiler that func() must receive at least three integer argument or parameters and then an unknown number (including 0) of parameters or arguments can have.

How to use variable number of arguments in C ?

Before use variable length arguments in C, you must include the header file named <stdarg.h>. This header file simply provides the functions and macros to implement the functionality of variable length arguments.

C Variable Arguments Example

Here some examples are listed to understand how variable length arguments works in a C program.

/* C Variable Length Arguments - This program
 * demonstrates variable length arguments in C
 */

#include<stdio.h>
#include<stdarg.h>
#include<conio.h>

int avg(int num, ...)
{
	va_list valist;
	int sum = 0;
	int i;

	// initializing valist for num number of arguments
	va_start(valist, num);

	// now accessing all the arguments that is assigned to valist
	for(i=0; i<num; i++)
	{
		sum = sum + va_arg(valist, int);
	}

	// now cleaning the memory reserved for the valist
	va_end(valist);

	return sum/num;
}

void main()
{
	int num1=10, num2=20, num3=30;
	clrscr();

	printf("The average is %d", avg(3, num1, num2, num3));

	getch();
}

Here is the sample output of the above C program:

c variable length arguments

Here the first parameter or argument in avg() tells the compiler that how many argument is going to pass to the function avg(). Above one is a simple variable length argument program. You also can pass more than or less than 3 arguments in the function avg(). Here is one more example:

/* C Variable Length Arguments */

#include<stdio.h>
#include<stdarg.h>
#include<conio.h>

int avg(int num, ...)
{
	va_list valist;
	int sum = 0;
	int i;

	va_start(valist, num);

	for(i=0; i<num; i++)
	{
		sum = sum + va_arg(valist, int);
	}

	va_end(valist);

	return sum/num;
}

void main()
{
	int num1, num2, num3, num4, num5;
	int avg1, avg2;
	clrscr();

	printf("Enter any three numbers to find average of all: ");
	scanf("%d%d%d", &num1, &num2, &num3);
	avg1 = avg(3, num1, num2, num3);

	printf("Enter any five numbers to find average of all: ");
	scanf("%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5);
	avg2 = avg(5, num1, num2, num3, num4, num5);

	printf("The average of %d, %d, and %d = %d", avg1);
	printf("\nThe average of %d, %d, %d, %d, and %d = %d", num1, num2, num3, num4, num5, avg2);

	getch();
}

Below is the sample output of this C program:

c variable length parameters

As you can see from the above output, the same function is used with variable length arguments. With 3 arguments and then with 5 arguments. Here is one another example:

/* C Variable Length Arguments */

#include<stdio.h>
#include<stdarg.h>
#include<conio.h>

int avg(int num, ...)
{
	va_list valist;
	int sum = 0;
	int i;

	va_start(valist, num);

	for(i=0; i<num; i++)
	{
		sum = sum + va_arg(valist, int);
	}

	va_end(valist);

	return sum/num;
}

void main()
{
	int arr[20], i, num;
	int mean, sum=0, count=0;
	clrscr();

	printf("How many number you want to enter ? ");
	scanf("%d", &num);
	printf("Enter %d number to find average of all: ", num);
	for(i=0; i<num; i++)
	{
		scanf("%d", &arr[i]);
	}

	for(i=0; i<num; i++)
	{
		sum = sum + avg(1, arr[i]);
		count++;
	}

	mean = sum/count;
	printf("The average is %d", mean);

	getch();
}

Here is the two sample run of the above C program:

variable length arguments in c

variable length arguments example

Here is another example.

/* C Variable Length Arguments */

#include<stdio.h>
#include<stdarg.h>
#include<conio.h>

int tot(int x,...)
{
	va_list valist;
	int sum = 0;
	int i;

	va_start(valist, x);

	for(i=0; i<x; i++)
	{
		sum = sum + va_arg(valist, int);
	}

	va_end(valist);
	return sum/x;
}

void main()
{
	int math, phys, chem, comp, eng;
	int perc;
	clrscr();

	printf("Enter marks obtained in the five subjects: ");
	scanf("%d%d%d%d%d",&math, &phys, &chem, &comp, &eng);
	printf("\nAverage Marks = %d\n", tot(5, math, phys, chem, comp, eng));

	getch();
}

Here is the sample run of the above C program:

variable length arguments programs

Let's look at one more example. This program also takes variable arguments, but this time user will enter the total number of arguments and then arguments value at the time of running the program. Just concentrate on this program for complete understanding on variable arguments.

/* C Variable Length Arguments */

#include<stdio.h>
#include<stdarg.h>
#include<conio.h>

double tot(int a,...)
{
	va_list valist;
	double sum = 0.0;
	int i;

	va_start(valist, a);

	for(i=0; i<a; i++)
	{
		sum = sum + va_arg(valist, int);
	}

	va_end(valist);
	return sum/a;
}

void main()
{
	int sub[50], num, i, count = 0;
	double sum = 0;
	clrscr();

	printf("Enter number of subjects: ");
	scanf("%d",&num);
	printf("Enter marks obtained in %d subjects: ", num);
	for(i=0; i<num; i++)
	{
		scanf("%d", &sub[i]);
		sum = sum + tot(1, sub[i]);
		count++;
	}
	sum = sum/count;
	printf("Average Marks = %f\n", sum);

	getch();
}

Here is the two sample run of the above C program:

c variable length arguments program

c variable length arguments example

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test