SlideShare a Scribd company logo
2
Most read
4
Most read
8
Most read
Variadic Functions
Introduction
 A function which takes a variable number or type of
arguments is called variadic function.
 Also called as Varargs function (or) Variable
Argument Lists.
 variadic functions are declared using a partial
parameter list followed by the ellipsis notation.
 A variadic function is invoked simply by specifying
the desired number of arguments in the function call.
E.g. average(3, 5, 8, -1).
 Most common variable argument
functions are
 void printf(const char* fmt, …);
 void scanf(const char* fmt, …);
 Parameters include
 One named argument usually a format
string.
 Ellipsis indicating the function may
take any number of arguments and their
types.
Creating a Variadic Function
 Use standard header stdarg.h
 4 macros (not prototypes) needed from this
header file
 va_list
 va_start
 va_args
 va_end
Variable arguments
 va_list
 Stores the list of arguments (argument pointer) and be
defined for variables used to traverse the list.
 Declared like any other variable.
 Ex. va_list ap
 va_start
 initializes the list, point to the first argument.
 Accepts two arguments.
o va_list.
o Name of the variable that directly precedes the
ellipsis.
 Ex. va_start(ap, fmt)
 va_args
 returns the next argument of whatever type it is
told.
 moves down to the next argument in the list.
 Accepts two arguments
o va_list
o Variable argument type
 Ex. va_args(ap, int) //returns next int value in the
argument
 va_end
 Cleans up the variable argument list.
 Ex. va_end(ap)
Example1
#include <stdarg.h>
#include <stdio.h>
int add_em_up (int count,...)
{
va_list ap;
int i, sum;
va_start (ap, count); /* Initialize the argument list. */
sum = 0;
for (i = 0; i < count; i++)
sum += va_arg (ap, int); /* Get the next argument value. */
va_end (ap); /* Clean up. */
return sum;
}
int main (void)
{
printf ("%dn", add_em_up (3,5,5,6));
printf ("%dn", add_em_up (10,1,2,3,4,5,6,7,8,9,10));
return 0;
}
Example2
#include <stdio.h>
#include <stdarg.h>
void simple_printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
while (*fmt != '0') {
if (*fmt == 'd') {
int i = va_arg(args, int);
printf( "%dn", i ) ;
} else if (*fmt == 'c') {
int c = va_arg(args, int);
printf( "%cn", (char)c ) ;
} else if (*fmt == 'f') {
double d = va_arg(args, double);
printf( "%fn", d ) ;
}
++fmt;
}
va_end(args);
}
int main()
{
simple_printf("dcff", 3, 'a', 1.999, 42.5);
}
Variadic functions

More Related Content

PPTX
Pointer to function 1
PPT
Switch statements in Java
PPTX
PPTX
Structure of c_program_to_input_output
PPTX
Function Pointer
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
PPT
Introduction to C Programming
PPT
How to execute a C program
Pointer to function 1
Switch statements in Java
Structure of c_program_to_input_output
Function Pointer
CONDITIONAL STATEMENT IN C LANGUAGE
Introduction to C Programming
How to execute a C program

What's hot (20)

DOC
Jumping statements
PPTX
Storage class in C Language
PPTX
C language
PPTX
C Programming: Control Structure
PPT
FUNCTIONS IN c++ PPT
PPT
Basics of C programming
PPSX
Data Types & Variables in JAVA
PPT
structure and union
PPTX
Python basics
PDF
Python programming : Strings
PDF
Exception handling
PPTX
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPT
Operators in C Programming
PPTX
Function overloading
PPTX
Strings in C language
PPSX
C lecture 4 nested loops and jumping statements slideshare
PPT
Variables in C Programming
PPTX
PPTX
Data Types and Variables In C Programming
Jumping statements
Storage class in C Language
C language
C Programming: Control Structure
FUNCTIONS IN c++ PPT
Basics of C programming
Data Types & Variables in JAVA
structure and union
Python basics
Python programming : Strings
Exception handling
DYNAMIC MEMORY ALLOCATION.pptx
Operators in C Programming
Function overloading
Strings in C language
C lecture 4 nested loops and jumping statements slideshare
Variables in C Programming
Data Types and Variables In C Programming
Ad

Similar to Variadic functions (20)

PPTX
Variadic functions
PDF
C Programming - Refresher - Part II
PPTX
C concepts and programming examples for beginners
PPTX
PPT
functions in cfunctions in cfunctions in c
PPT
PPTX
EST 102 Programming in C-MODULE 4
PPT
Functions in c
PPTX
Functions in C
PPT
funcrttionadl asfkjoefweno alsdfjoweefwf
DOC
Unit 4 (1)
PPTX
unit_2.pptx
PPT
RECURSION IN C
PPT
Recursion in C
PPTX
Presentation on function
PPT
C programming is a powerful, general-purpose language used for developing ope...
PDF
VIT351 Software Development VI Unit1
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
PDF
Principals of Programming in CModule -5.pdfModule-3.pdf
PPTX
CH.4FUNCTIONS IN C (1).pptx
Variadic functions
C Programming - Refresher - Part II
C concepts and programming examples for beginners
functions in cfunctions in cfunctions in c
EST 102 Programming in C-MODULE 4
Functions in c
Functions in C
funcrttionadl asfkjoefweno alsdfjoweefwf
Unit 4 (1)
unit_2.pptx
RECURSION IN C
Recursion in C
Presentation on function
C programming is a powerful, general-purpose language used for developing ope...
VIT351 Software Development VI Unit1
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
Principals of Programming in CModule -5.pdfModule-3.pdf
CH.4FUNCTIONS IN C (1).pptx
Ad

More from Koganti Ravikumar (7)

PPT
Advanced pointers
PPT
Preprocessors
PPTX
PPT
PPT
Linker scripts

Recently uploaded (20)

PPTX
An introduction to AI in research and reference management
PDF
Quality Control Management for RMG, Level- 4, Certificate
PPTX
LITERATURE CASE STUDY DESIGN SEMESTER 5.pptx
PDF
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
PPTX
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
PDF
Test slideshare presentation for blog post
PDF
Phone away, tabs closed: No multitasking
PPT
pump pump is a mechanism that is used to transfer a liquid from one place to ...
PDF
SEVA- Fashion designing-Presentation.pdf
PDF
Interior Structure and Construction A1 NGYANQI
PPTX
NEW EIA PART B - Group 5 (Section 50).pptx
PDF
Introduction-to-World-Schools-format-guide.pdf
PPTX
Special finishes, classification and types, explanation
PDF
BRANDBOOK-Presidential Award Scheme-Kenya-2023
PPTX
building Planning Overview for step wise design.pptx
PDF
Pongal 2026 Sponsorship Presentation - Bhopal Tamil Sangam
PPTX
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
PPTX
CLASSIFICATION OF YARN- process, explanation
PPT
WHY_R12 Uaafafafpgradeaffafafafaffff.ppt
PPTX
AD Bungalow Case studies Sem 2.pptxvwewev
An introduction to AI in research and reference management
Quality Control Management for RMG, Level- 4, Certificate
LITERATURE CASE STUDY DESIGN SEMESTER 5.pptx
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
Test slideshare presentation for blog post
Phone away, tabs closed: No multitasking
pump pump is a mechanism that is used to transfer a liquid from one place to ...
SEVA- Fashion designing-Presentation.pdf
Interior Structure and Construction A1 NGYANQI
NEW EIA PART B - Group 5 (Section 50).pptx
Introduction-to-World-Schools-format-guide.pdf
Special finishes, classification and types, explanation
BRANDBOOK-Presidential Award Scheme-Kenya-2023
building Planning Overview for step wise design.pptx
Pongal 2026 Sponsorship Presentation - Bhopal Tamil Sangam
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
CLASSIFICATION OF YARN- process, explanation
WHY_R12 Uaafafafpgradeaffafafafaffff.ppt
AD Bungalow Case studies Sem 2.pptxvwewev

Variadic functions

  • 2. Introduction  A function which takes a variable number or type of arguments is called variadic function.  Also called as Varargs function (or) Variable Argument Lists.  variadic functions are declared using a partial parameter list followed by the ellipsis notation.  A variadic function is invoked simply by specifying the desired number of arguments in the function call. E.g. average(3, 5, 8, -1).
  • 3.  Most common variable argument functions are  void printf(const char* fmt, …);  void scanf(const char* fmt, …);  Parameters include  One named argument usually a format string.  Ellipsis indicating the function may take any number of arguments and their types.
  • 4. Creating a Variadic Function  Use standard header stdarg.h  4 macros (not prototypes) needed from this header file  va_list  va_start  va_args  va_end
  • 5. Variable arguments  va_list  Stores the list of arguments (argument pointer) and be defined for variables used to traverse the list.  Declared like any other variable.  Ex. va_list ap  va_start  initializes the list, point to the first argument.  Accepts two arguments. o va_list. o Name of the variable that directly precedes the ellipsis.  Ex. va_start(ap, fmt)
  • 6.  va_args  returns the next argument of whatever type it is told.  moves down to the next argument in the list.  Accepts two arguments o va_list o Variable argument type  Ex. va_args(ap, int) //returns next int value in the argument  va_end  Cleans up the variable argument list.  Ex. va_end(ap)
  • 7. Example1 #include <stdarg.h> #include <stdio.h> int add_em_up (int count,...) { va_list ap; int i, sum; va_start (ap, count); /* Initialize the argument list. */ sum = 0; for (i = 0; i < count; i++) sum += va_arg (ap, int); /* Get the next argument value. */ va_end (ap); /* Clean up. */ return sum; } int main (void) { printf ("%dn", add_em_up (3,5,5,6)); printf ("%dn", add_em_up (10,1,2,3,4,5,6,7,8,9,10)); return 0; }
  • 8. Example2 #include <stdio.h> #include <stdarg.h> void simple_printf(const char *fmt, ...) { va_list args; va_start(args, fmt); while (*fmt != '0') { if (*fmt == 'd') { int i = va_arg(args, int); printf( "%dn", i ) ; } else if (*fmt == 'c') { int c = va_arg(args, int); printf( "%cn", (char)c ) ; } else if (*fmt == 'f') { double d = va_arg(args, double); printf( "%fn", d ) ; } ++fmt; } va_end(args); } int main() { simple_printf("dcff", 3, 'a', 1.999, 42.5); }