Variadic functions in C

04 Jun, 2022
Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed. The variadic function consists of at least one fixed variable and then an ellipsis(...) as the last parameter.
Syntax:
int function_name(data_type variable_name, ...);
Values of the passed arguments can be accessed through the header file named as:
#include <stdarg.h>
<stdarg.h> includes the following methods:
Methods
Description
Here, va_list holds the information needed by va_start, va_arg, va_end, and va_copy.
Program 1:
The following simple C program will demonstrate the working of the variadic function AddNumbers():
C
// C program for the above approach #include <stdarg.h>#include <stdio.h> // Variadic function to add numbersint AddNumbers(int n, ...){    int Sum = 0;     // Declaring pointer to the    // argument list    va_list ptr;     // Initializing argument to the    // list pointer    va_start(ptr, n);     for (int i = 0; i < n; i++)         // Accessing current variable        // and pointing to next one        Sum += va_arg(ptr, int);     // Ending argument list traversal    va_end(ptr);     return Sum;} // Driver Codeint main(){    printf("\n\n Variadic functions: \n");     // Variable number of arguments    printf("\n 1 + 2 = %d ",           AddNumbers(2, 1, 2));     printf("\n 3 + 4 + 5 = %d ",           AddNumbers(3, 3, 4, 5));     printf("\n 6 + 7 + 8 + 9 = %d ",           AddNumbers(4, 6, 7, 8, 9));     printf("\n");     return 0;}
Variadic functions: 

 1 + 2 = 3 
 3 + 4 + 5 = 12 
 6 + 7 + 8 + 9 = 30
 
Program 2: Below is the C program consisting of the variadic function LargestNumber():
C
// C program for the above approach#include <stdarg.h>#include <stdio.h> // Variadic function to find the largest numberint LargestNumber(int n, ...){    int max = 0;     // Declaring pointer to the    // argument list    va_list ptr;     // Initializing argument to the    // list pointer    va_start(ptr, n);     for (int i = 0; i < n; i++) {         // Accessing current variable        // and pointing to next        int temp = va_arg(ptr, int);        max = temp > max ? temp : max;    }     // End of argument list traversal    va_end(ptr);     return max;} // Driver Codeint main(){    printf("\n\n Variadic functions: \n");     // Variable number of arguments    printf("\n %d ",           LargestNumber(2, 1, 2));     printf("\n %d ",           LargestNumber(3, 3, 4, 5));     printf("\n %d ",           LargestNumber(4, 6, 7, 8, 9));     printf("\n");     return 0;}
Variadic functions: 

 2 
 5 
 9
 
anikakapoor
sagartomar9927
tauleshwar
C Basics
C-Functions
Functions
C++
C++ Programs
Functions
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
Shallow Copy and Deep Copy in C++