Variable Arguments in C Language with Examples

C Variable Arguments: Sometimes you might need a function that can take any number of arguments, or parameters.

Using the C programming language, you can build a function that can take any number of parameters.

The definition of such a function is seen in the example below.

int function(int, ... ) {
   .
   .
   .
}

int main() {
   function(1, 2, 3);
   function(1, 2, 3, 4);
}

What is Variable Arguments in C?

A feature known as “variable length argument” makes it possible for a function to take in an arbitrary number of arguments.

There are times when we need a function that can handle a different number of arguments depending on what the situation calls for.

How are arguments passed in C?

At the moment of function execution, passing variable arguments in c are copied to the program stack, where they wait to be read by the respective function.

These arguments can either be values in their own right or pointers to sections of memory that hold the data that is being sent along.

Either way, they can either be values or pointers. The process of passing a value via reference is another name for passing a pointer.

Syntax: variable argument function in c

  • Last is the last function parameter before the ellipsis (“…”) in void call va_start(va_list ap, last); /* Begin processing of variadic functions.

  • Get the following variadic argument from the list using type va_arg(va_list ap, type); * Pass the proper promoted type */.

  • void va_end(va_list ap); * Finish processing arguments

  • Va _ist src, void va copy(va_list dst);

  • Copy the current argument list (or the current position in the argument processing) to another list in C99 or later.

Parameters: C variable arguments

ParameterDetails
va_list aplocation in the list of variable parameters, argument pointer
lastname of the most recent non-variadic function parameter, which helps the compiler determine where to begin processing the variadic arguments; cannot be declared as an array type, a function, or a register variable.
typeread the promoted type of the variadic argument (e.g. int for a short int argument)
va_list srcpointer to the current argument to copy
va_list dstfurther arguments must be added

The function() contains three dots (…) as its last argument, which represents the total number of variable arguments supplied.

  • Use the stdarg.h: The header file provides call functions and macros to implement the functionality of variable number of arguments. To use this capability, follow the procedures provided.

  • Create a function that takes an int as its first argument and an ellipsis as its final argument.

  • Add a variable of type void va start va list to the function declaration. This type is specified in the header file include stdarg.h.

  • To initialize the va list variable, use the va start va arg macro with the int parameter.

  • To access each item in the variable argument list, use the va arg macro with the va list variable.

  • To free up the memory allotted to the va list variable, use the va end macro.

We can create a function arguments that takes a configurable number of parameters and returns their average.

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

double average(int number,...) {

   va_list variableList;
   double sum = 0.0;
   int x;

   /* initialize variableList for num number of arguments */
   va_start(variableList, number);

   /* access all the arguments assigned to variableList */
   for (x = 0; x < number; x++) {
      sum += va_arg(variableList, int);
   }
	
   /* clean memory reserved for variableList */
   va_end(variableList);

   return sum/number;
}

int main() {
   printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5));
   printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15));
}

The result of compiling and running the example code is as follows.

It should be observed that average() has been called twice, each time with a different number of parameters.

Only ellipses will be used to pass arguments with a variable number.

Average of 2, 3, 4, 5 = 3.500000
Average of 5, 10, 15 = 10.000000

You can test the above example here! ➡ C Online Compiler

Summary

This tutorial taught us about C Variable Arguments and how to use them using real-world examples. I sincerely hope you enjoy this tutorial.

About Next Tutorial

In the following post, I’ll create a Memory Management in C and try to explain its many components in full detail. This post is about variable arguments in C language with examples. I hope you enjoy it!


Leave a Comment