Quick links
In the journey of learning C functions, we learned many concepts related to functions. We learned to define our own function, passing arguments to a function, returning value from a function, recursive function etc. In this chapter, I will talk something interesting about passing variable length arguments to a function.
Have you ever wondered how functions like printf()
and scanf()
works? As they readily accept any number of arguments passed. You can say –
In real you can pass n number of arguments to printf()
, but how it works?
What is variable length arguments (var-args)?
In programming, there happens situation when you want your function to accept variable number of arguments. For example – suppose I ask you to write a function to find maximum. You will end up with function declaration similar to
However, none of the above declaration is suitable for the case. First function will find maximum between three numbers likewise second will find maximum of five numbers. What if I need to find maximum between four, ten or sometime n numbers. For such case, we use variable length arguments in a function.
Variable length arguments is a programming construct that allows programmers to pass n number of arguments to a function. You can also call variable length argument as var-args.
Syntax of using variable length arguments
- Return type – Specify function return type.
- Function name – Name of the function.
- Parameter list – This is optional for var-args function. If your function accepts parameter other than var-args, then pass it before var-args.
- int num –
int num
specify number of variable length arguments passed. - Ellipsis symbol … – Specify that the function is ready to accept n number of arguments.
Macros used in variable length arguments
You cannot access variable length arguments directly. Header file stdarg.h
provide macros that support access to var-args. To implement var-args in our program we will use following macros definitions.
va_list
– Data type to define ava_list
type variable.va_start
– Used to initializeva_list
type variable.va_arg
– Retrieves next value from theva_list
type variable.va_end
– Release memory assigned to ava_list
variable.
Note: Do not confuse yourself with the word macro. For now think it as a function, I will cover it separately in later sections.
How to use variable length arguments in a function?
Follow six simple steps to implement variable length arguments in your function.
- Include
stdarg.h
header file to access variable length argument. - Define a function to accept variable length arguments.
- Inside function create a
va_list
type variable. - Initialize list variable using
va_start
macro.va_start
macro accepts two parameters. Firstva_list
type variable and number of arguments in the list. - Run a loop from 1 to number of arguments. Inside the loop, use
va_arg
to get next argument passed as variable length arguments.va_arg
accepts two parameter. Firstva_list
type variable from where to fetch values. Second, type (data type) of value you want to retrieve. - Finally release memory occupied by
va_list
usingva_end
.
Example program to use variable length arguments
Write a C function to accept n number of arguments using variable length arguments. Return maximum of all values.
Output –
Limitations of variable length arguments
Variable length arguments exhibits a great advantage of passing n number of arguments to a function. However, there are few limitations of using var-args.
- Arguments passed as var-args must be of same type.
- You must pass variable length arguments as last parameter. For example –
Despite of its own advantages and disadvantages, I recommend you make less use variable length arguments. You can easily replace any variable length arguments function by passing array to a function.