Can a Function have a Variable Number of Arguments in C?

Question: Can a Function in C Programming have Variable Number of Arguments?

Answer: Of course! Functions in a C program can have variable number of arguments. let’s first explore a very familiar standard C library output function ‘printf()’ below,

/*
 * fun_with_varible_arg1.c -- program shows function takes variable number
 * of arguments
 */
#include <stdio.h>
int main(void)
{
    int num1 = 10, num2 = 20;
 
    printf("\nWe are familiar with printf() and scanf() functions which\n"
           "accept variable number of arguments!\n\n");
 
    printf("First argument is String.\n");
    printf("Successive arguments are comma separated list and type "
           "specifiers\nfor each in Format String.\n\n");
 
    printf("Sum of %d and %d is %d\n", num1, num2, num1 + num2);
                                            /* four arguments */
    printf("Numbers %d, %d, %d, %d, %d, %d are all Prime Numbers!\n",
            2, 3, 5, 7, 11, 13);            /* six arguments */
    printf("\n");
 
    return 0;
}
 
Output produced when we compiled and run the above program as,
 
<pre lang="C" cssfile="hk1_style">
 
We are familiar with printf() and scanf() functions which
accept variable number of arguments!
 
First argument is String.
Successive arguments are comma separated list and type specifiers
for each in Format String.
 
Sum of 10 and 20 is 30
Numbers 2, 3, 5, 7, 11, 13 are all Prime Numbers!

Likewise, ‘scanf()’ function takes variable number of arguments. Now, we turn to ours’ concern about how can we declare and define such function that accepts variable no. of arguments.

The need for a function which accepts variable number of arguments arises where we need to pass different number and types of arguments to a function. Function with variable arguments can be implemented using macros. These macros are defined in stdarg.h header which is part of standard library. stdarg header declares a type called va_list and three macros viz. va_start, va_arg and va_end. The variable of type va_list is used with three macros to access the variable arguments.

Prototyping such a function is done as follows:

    type name_function(type no_of_arguments,...);
    /* note three ellipsis specify variable argument list */

Function definition header is given as below:

advertisement
advertisement
    /* note three ellipsis specify var. arg. list */
    type fun_name(type no_of_values, ...)
    {
 
    }

Let’s now consider an example,

/*
 * fun_var_arg_imp_stdarg.c -- program shows how a function with variable
 * arguments can be implemented
 */
#include <stdio.h>
#include <stdarg.h>		 /* macros are declared therein */
double average(int n_values, ...);      /* prototyping function */
/* three ellipsis indicate unspecified arguments with unspecified types */
 
int main(void)
{
    double av;
 
    av = average(5, 1, 2, 3, 4, 5);	/* calling the function */
    printf("average of 1,2,3,4,5 is %lf\n\n", av);
    return 0;
}
 
double average(int num_values, ...)
{
    va_list varg;     /* declared varg of type va_list*/
    int count, sum = 0;
 
    /* initialize varg to point to first of variable arguments */
    va_start(varg, num_values);
    printf("\n");
 
    for (count = 0; count < num_values; count++ ) {
    /* to access value of variable arguments, call macro va_arg() */
        sum += va_arg(varg, int);
        printf("sum is %d\n", sum);
    }
    printf("\n");
    return (double)sum / num_values;
}

Let’s analyse the output when function is called as:

Note: Join free Sanfoundry classes at Telegram or Youtube
av = average(5, 1, 2, 3, 4, 5);
/* first argument specifies 5 variable integer arguments  */

output is follows:

sum is 1
sum is 3
sum is 6
sum is 10
sum is 15
 
average of 1,2,3,4,5 is 3.000000

…what if we have had called the function as:

advertisement
av = average(2, 1, 2, 3, 4, 5);

then output followed as:

sum is 1
sum is 3
 
average of 1,2,3,4,5 is 1.500000

and when we had called the function as:

av = average(6, 1, 2, 3, 4, 5);

output then followed as:

advertisement
sum is 1
sum is 3
sum is 6
sum is 10
sum is 15
sum is -218509745
 
average of 1,2,3,4,5 is -36418292.000000

Limitations of Functions with variable arguments:

1. Macros can’t determine number of variable arguments
2. Type of a variable argument can’t be determined by examining the bits of argument.

The first named argument answers both specifying number and types of arguments. In the above program, named argument num_values specify number of variable arguments and their types assumed to be integers.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.