What are Variadic Functions in C?
A variadic function in C is a function that can accept a variable number of arguments. This allows flexibility in function calls where the number of parameters may not be known at compile time. The most common example is the printf() function.
Required Header:
To create a variadic function, include:
#include <stdarg.h>
This header provides the macros necessary to work with variable arguments.
Syntax of a Variadic Function
#include <stdarg.h> return_type function_name(fixed_params, ...);
Inside the function:
va_list var_name; va_start(var_name, last_fixed_param); type value = va_arg(var_name, type); ... va_end(var_name);
Working with Variable Arguments
Macro | Description |
---|---|
va_list | Type to hold the variable argument list |
va_start | Initializes the va_list |
va_arg | Retrieves the next argument |
va_end | Cleans up the va_list |
Note: You must use the correct type in va_arg(). There is no type checking with variadic arguments, and incorrect usage may lead to undefined behavior.
Example: Variadic Function to Sum Integers
#include <stdio.h> #include <stdarg.h> int sum_quiz(int count, ...) { int total = 0; va_list quiz; va_start(quiz, count); for (int i = 0; i < count; i++) { total += va_arg(quiz, int); } va_end(quiz); return total; } int main() { int result = sum_quiz(4, 10, 20, 30, 40); printf("Sum of test values: %d\n", result); return 0; }
Output:
Sum of test values: 100
This C program shows how to use a function with a variable number of arguments. The sum_quiz function takes a count and then adds that many numbers using macros from <stdarg.h>. It uses va_start to begin reading the extra values, va_arg to get each one, and va_end to finish. In main, the function adds 10, 20, 30, and 40, then prints the total. The output is the sum: 100.
Example 2: Calculating Average with double
double average(int count, ...) { va_list args; double sum = 0; va_start(args, count); for (int i = 0; i < count; i++) { sum += va_arg(args, double); } va_end(args); return sum / count; }
This C function calculates the average of a variable number of double values. It uses va_list to handle the arguments and starts reading them with va_start. Inside a loop, it adds each value using va_arg, then ends with va_end. The function returns the total sum divided by the count.
Common Variadic Functions in C Standard Library
- printf()
- scanf()
- vprintf(), vfprintf(), vsprintf()
Limitations of Variadic Functions
- No type safety: Compiler can’t check argument types.
- Hard to debug: Mistakes in argument types or count can lead to crashes.
- Performance: Slight overhead due to argument handling.
Use Cases of Variadic Functions
1. printf Function: The most common example of a variadic function in C is printf. The printf function can accept any number of arguments, depending on the format string passed to it. The function uses the stdarg.h macros to access and format these arguments.
Example:
printf("Hello %s, you have %d new messages", "Sammy", 5);
2. Logging Functions: Variadic functions are often used in logging systems where the number of variables to log may differ depending on the context.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check Computer Science Books
- Check C Books
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship