The Wayback Machine - https://web.archive.org/web/20240717024800/https://www.geeksforgeeks.org/variadic-functions-in-c/
Open In App

Variadic functions in C

Last Updated : 25 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

va_start(va_list ap, argN)

This enables access to variadic function arguments.

where *va_list* will be the pointer to the last fixed argument in the variadic function

*argN* is the last fixed argument in the variadic function. 

From the above variadic function (function_name (data_type variable_name, …);), variable_name is the last fixed argument making it the argN. Whereas *va_list ap* will be a pointer to argN (variable_name)

va_arg(va_list ap, type)

This one accesses the next variadic function argument.

*va_list ap* is the same as above i.e a pointer to argN

*type* indicates the data type  the *va_list ap* should expect (double, float, int etc.)

va_copy(va_list dest, va_list src) This makes a copy of the variadic function arguments.
va_end(va_list ap) This ends the traversal of the variadic function arguments.

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 numbers
int 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 Code
int 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;
}


Output: 

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 number
int LargestNumber(int n, ...)
{
      // Declaring pointer to the
    // argument list
    va_list ptr;
 
    // Initializing argument to the
    // list pointer
    va_start(ptr, n);
   
    int max = va_arg(ptr, int);
 
    for (int i = 0; i < n-1; 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 Code
int 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;
}


Output: 

Variadic functions: 

 2 
 5 
 9

 



Previous Article
Next Article

Similar Reads

Variadic function templates in C++
Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue. Douglas Gregor and Jaakko Järvi came up with this feature for C+
3 min read
SQL Functions (Aggregate and Scalar Functions)
SQL Functions are built-in programs that are used to perform different operations on the database. There are two types of functions in SQL: Aggregate FunctionsScalar FunctionsSQL Aggregate FunctionsSQL Aggregate Functions operate on a data group and return a singular output. They are mostly used with the GROUP BY clause to summarize data.  Some com
4 min read
Virtual Functions in Derived Classes in C++
A virtual function is a member function of a base class that is overridden by a derived class. When you use a pointer or a reference to the base class to refer to a derived class object, you can call a virtual function for that object and have it run the derived class's version of the function. In C++, once a member function is declared as a virtua
2 min read
Can Virtual Functions be Private in C++?
A virtual function can be private as C++ has access control, but not visibility control. As mentioned virtual functions can be overridden by the derived class but under all circumstances will only be called within the base class. Example: C/C++ Code // C++ program to demonstrate how a // virtual function can be private #include &lt;iostream&gt; cla
3 min read
Virtual Functions and Runtime Polymorphism in C++
A virtual function is a member function that is declared in the base class using the keyword virtual and is re-defined (Overridden) in the derived class. It tells the compiler to perform late binding where the compiler matches the object with the right called function and executes it during the runtime. This technique falls under Runtime Polymorphi
8 min read
Builtin functions of GCC compiler
These are four important built-in functions in GCC compiler: 1. __builtin_popcount(x) This function is used to count the number of one's(set bits) in an integer. Example: if x = 4 binary value of 4 is 100 Output: No of ones is 1. C/C++ Code // C program to illustrate _builtin_popcount(x) #include &lt;stdio.h&gt; int main() { int n = 5; printf(
3 min read
Python | Functions | Question 1
What will be the output of the following code : print type(type(int)) (A) type 'int' (B) type 'type' (C) Error (D) 0 Answer: (B) Explanation: The type() function returns the class of the argument the object belongs to. Thus, type(int) returns which is of the type ‘type’ object.Quiz of this Question
1 min read
Python | Functions | Question 2
What is the output of the following code : L = ['a','b','c','d'] print(&quot;&quot;.join(L)) (A) Error (B) None (C) abcd (D) [‘a’,’b’,’c’,’d’] Answer: (C) Explanation: “” depicts a null string and the join function combines the elements of the list into a string.Quiz of this Question
1 min read
Python | Functions | Question 3
What is the output of the following segment : chr(ord('A')) (A) A (B) B (C) a (D) Error Answer: (A) Explanation: ord() function converts a character into its ASCII notation and chr() converts the ASCII to character.Quiz of this Question
1 min read
Python | Functions | Question 4
What is the output of the following program : y = 8 z = lambda x : x * y print (z(6)) (A) 48 (B) 14 (C) 64 (D) None of the above Answer: (A) Explanation: lambdas are concise functions and thus, result = 6 * 8Quiz of this Question
1 min read
Practice Tags :
three90RightbarBannerImg