You are on page 1of 1

Courses Tutorials Jobs Practice Sign In

Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python Competitive Programming Machine Learning HTML SDE Sheet Puzzles GFG School Projects

Variadic functions in C
Difficulty Level :
Hard ● Last Updated :
04 Jun, 2022

Variadic functions are functions that can take a variable number of argument s. In C programming, a variadic function adds flexibilit y to

the program. It takes one fixed argument and then any number of argument s can be passed. The variadic function consist s 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 argument s can be accessed through the header file named as:

#include <stdarg.h>

<stdarg.h> includes the following methods:

Methods Description

va_star t(va_list ap, argN) This enables access to variadic function argument s.

va_arg(va_list ap, t ype) This one accesses the next variadic function argument.

va_copy(va_list dest, va_list src) This makes a copy of the variadic function argument s.

va_end(va_list ap) This ends the traversal of the variadic function argument s.

Here, va_list holds the information needed by va_star t, va_arg, va_end, and va_copy.

Program 1:

The following simple C program will demonstrate the working of the variadic function AddNumbers():
WHAT'S NEW

C++ Programming Foundation- Self Paced


C
Course

// C program for the above approach View Details


 
#include <stdarg.h> Geek-O-Lympics 2022
#include <stdio.h>
 
// Variadic function to add numbers
View Details
int AddNumbers(int n, ...)
{
    int Sum = 0; Complete Interview Preparation- Self Paced
  Course
    // Declaring pointer to the
    // argument list View Details
    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 L argestNumber():

// C program for the above approach


#include <stdarg.h>
#include <stdio.h>
 
// Variadic function to find the largest number
int 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 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:

Like 21

Previous Next

DataTables pageLength Option Difference between Dangling pointer and Void


pointer

RECOMMENDED ARTICLES Page : 1 2 3

Variadic function templates in C++ Pure Functions


01 10, Jan 17
05 10, Dec 11

Static functions in C
02 05, May 10
Can Virtual Functions be Inlined in C++?
06 10, May 14

Some interesting facts about static member functions


03
in C++ Return From Void Functions in C++
09, Jan 11 07 03, Jul 16

Functions that are executed before and after main() in


04
C Builtin functions of GCC compiler
30, Sep 11
08 23, Mar 17

Article Contributed B y : Vote for dif ficult y

Current difficulty :
Hard

opeeyum
@opeeyum Easy Normal Medium Hard Expert

Improved By : anikakapoor, sagartomar9927, tauleshwar


Article Tags : C Basics, C-Functions, Functions, C++, C++ Programs
Practice Tags : Functions, CPP

Improve Article Report Issue

Writing code in comment?


Please use ide.geeksforgeeks.org,
generate link and share the link here.

Load Comments

Company Learn News Languages Web Development Contribute


A-143, 9th Floor, Sovereign Corporate Tower, About Us Algorithms Python Web Tutorials Write an Article
Sector-136, Noida, Uttar Pradesh - 201305 Top News
feedback@geeksforgeeks.org Careers Data Structures Java Django Tutorial Improve an Article
Technology
In Media SDE Cheat Sheet CPP HTML Pick Topics to Write
Work & Career
Contact Us Machine learning Golang JavaScript Write Interview Experience
Business
Privacy Policy CS Subjects C# Bootstrap Internships
Finance
Copyright Policy Video Tutorials SQL ReactJS Video Internship
Lifestyle
Courses Kotlin NodeJS
Knowledge

@geeksforgeeks
, Some rights reserved

You might also like