SlideShare a Scribd company logo
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

What's hot

C standard library functions
C standard library functionsC standard library functions
C standard library functions
Vaishnavee Sharma
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
Kamal Acharya
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
Ashim Lamichhane
 
8085 alp programs
8085 alp programs8085 alp programs
What is CPU Register? Type of CPU Register.
What is CPU Register? Type of CPU Register.What is CPU Register? Type of CPU Register.
What is CPU Register? Type of CPU Register.
Kapil Dev Das
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
8086 memory segmentation
8086 memory segmentation8086 memory segmentation
8086 memory segmentation
mahalakshmimalini
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
Rajab Ali
 

What's hot (20)

C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Functions in C
Functions in CFunctions in C
Functions in C
 
File in C language
File in C languageFile in C language
File in C language
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Strings
StringsStrings
Strings
 
Inline function
Inline functionInline function
Inline function
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
8085 alp programs
8085 alp programs8085 alp programs
8085 alp programs
 
What is CPU Register? Type of CPU Register.
What is CPU Register? Type of CPU Register.What is CPU Register? Type of CPU Register.
What is CPU Register? Type of CPU Register.
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
8086 memory segmentation
8086 memory segmentation8086 memory segmentation
8086 memory segmentation
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 

Similar to Variadic functions

Variadic functions
Variadic functionsVariadic functions
Variadic functions
ramyaranjith
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 
functionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdffunctionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdf
mounikanarra3
 
The Task For this assignment you will write a rudimentary text edi.pdf
The Task For this assignment you will write a rudimentary text edi.pdfThe Task For this assignment you will write a rudimentary text edi.pdf
The Task For this assignment you will write a rudimentary text edi.pdf
info785431
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
Rai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
aneebkmct
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
Rai University
 
Tut1
Tut1Tut1
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
Rai University
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
Rai University
 
Chapter 13.1.7
Chapter 13.1.7Chapter 13.1.7
Chapter 13.1.7patcha535
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
Tia Ricci
 
U4.ppt
U4.pptU4.ppt
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
Muthuselvam RS
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
sholavanalli
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
awkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux adminawkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux admin
ZoumanaDiomande1
 

Similar to Variadic functions (20)

Variadic functions
Variadic functionsVariadic functions
Variadic functions
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 
functionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdffunctionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdf
 
The Task For this assignment you will write a rudimentary text edi.pdf
The Task For this assignment you will write a rudimentary text edi.pdfThe Task For this assignment you will write a rudimentary text edi.pdf
The Task For this assignment you will write a rudimentary text edi.pdf
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
Tut1
Tut1Tut1
Tut1
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
 
Chapter 13.1.7
Chapter 13.1.7Chapter 13.1.7
Chapter 13.1.7
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
U4.ppt
U4.pptU4.ppt
U4.ppt
 
C language presentation
C language presentationC language presentation
C language presentation
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Strings
StringsStrings
Strings
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
awkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux adminawkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux admin
 

More from Koganti Ravikumar (7)

Qemu
QemuQemu
Qemu
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Preprocessors
PreprocessorsPreprocessors
Preprocessors
 
Loaders
LoadersLoaders
Loaders
 
Linkers
LinkersLinkers
Linkers
 
Linker scripts
Linker scriptsLinker scripts
Linker scripts
 
ELF
ELFELF
ELF
 

Recently uploaded

一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证成绩单如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证成绩单如何办理一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证成绩单如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证成绩单如何办理
n0tivyq
 
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
 
Game Concept Presentation for Ukrainian Mythology Based Game With Designs
Game Concept Presentation for Ukrainian Mythology Based Game With DesignsGame Concept Presentation for Ukrainian Mythology Based Game With Designs
Game Concept Presentation for Ukrainian Mythology Based Game With Designs
184804
 
原版定做(penn毕业证书)美国宾夕法尼亚大学毕业证文凭学历证书原版一模一样
原版定做(penn毕业证书)美国宾夕法尼亚大学毕业证文凭学历证书原版一模一样原版定做(penn毕业证书)美国宾夕法尼亚大学毕业证文凭学历证书原版一模一样
原版定做(penn毕业证书)美国宾夕法尼亚大学毕业证文凭学历证书原版一模一样
gpffo76j
 
一比一原版(毕业证)长崎大学毕业证成绩单如何办理
一比一原版(毕业证)长崎大学毕业证成绩单如何办理一比一原版(毕业证)长崎大学毕业证成绩单如何办理
一比一原版(毕业证)长崎大学毕业证成绩单如何办理
taqyed
 
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
7sd8fier
 
一比一原版(UCB毕业证书)伯明翰大学学院毕业证成绩单如何办理
一比一原版(UCB毕业证书)伯明翰大学学院毕业证成绩单如何办理一比一原版(UCB毕业证书)伯明翰大学学院毕业证成绩单如何办理
一比一原版(UCB毕业证书)伯明翰大学学院毕业证成绩单如何办理
h7j5io0
 
Connect Conference 2022: Passive House - Economic and Environmental Solution...
Connect Conference 2022: Passive House -  Economic and Environmental Solution...Connect Conference 2022: Passive House -  Economic and Environmental Solution...
Connect Conference 2022: Passive House - Economic and Environmental Solution...
TE Studio
 
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
jyz59f4j
 
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANEEASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
Febless Hernane
 
Technoblade The Legacy of a Minecraft Legend.
Technoblade The Legacy of a Minecraft Legend.Technoblade The Legacy of a Minecraft Legend.
Technoblade The Legacy of a Minecraft Legend.
Techno Merch
 
一比一原版(Bristol毕业证书)布里斯托大学毕业证成绩单如何办理
一比一原版(Bristol毕业证书)布里斯托大学毕业证成绩单如何办理一比一原版(Bristol毕业证书)布里斯托大学毕业证成绩单如何办理
一比一原版(Bristol毕业证书)布里斯托大学毕业证成绩单如何办理
smpc3nvg
 
Expert Accessory Dwelling Unit (ADU) Drafting Services
Expert Accessory Dwelling Unit (ADU) Drafting ServicesExpert Accessory Dwelling Unit (ADU) Drafting Services
Expert Accessory Dwelling Unit (ADU) Drafting Services
ResDraft
 
一比一原版(Columbia毕业证)哥伦比亚大学毕业证如何办理
一比一原版(Columbia毕业证)哥伦比亚大学毕业证如何办理一比一原版(Columbia毕业证)哥伦比亚大学毕业证如何办理
一比一原版(Columbia毕业证)哥伦比亚大学毕业证如何办理
asuzyq
 
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
708pb191
 
一比一原版(UNUK毕业证书)诺丁汉大学毕业证如何办理
一比一原版(UNUK毕业证书)诺丁汉大学毕业证如何办理一比一原版(UNUK毕业证书)诺丁汉大学毕业证如何办理
一比一原版(UNUK毕业证书)诺丁汉大学毕业证如何办理
7sd8fier
 
Research 20 slides Amelia gavryliuks.pdf
Research 20 slides Amelia gavryliuks.pdfResearch 20 slides Amelia gavryliuks.pdf
Research 20 slides Amelia gavryliuks.pdf
ameli25062005
 
一比一原版(MMU毕业证书)曼彻斯特城市大学毕业证成绩单如何办理
一比一原版(MMU毕业证书)曼彻斯特城市大学毕业证成绩单如何办理一比一原版(MMU毕业证书)曼彻斯特城市大学毕业证成绩单如何办理
一比一原版(MMU毕业证书)曼彻斯特城市大学毕业证成绩单如何办理
7sd8fier
 
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
h7j5io0
 
projectreportnew-170307082323 nnnnnn(1).pdf
projectreportnew-170307082323 nnnnnn(1).pdfprojectreportnew-170307082323 nnnnnn(1).pdf
projectreportnew-170307082323 nnnnnn(1).pdf
farazahmadas6
 

Recently uploaded (20)

一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证成绩单如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证成绩单如何办理一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证成绩单如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证成绩单如何办理
 
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
 
Game Concept Presentation for Ukrainian Mythology Based Game With Designs
Game Concept Presentation for Ukrainian Mythology Based Game With DesignsGame Concept Presentation for Ukrainian Mythology Based Game With Designs
Game Concept Presentation for Ukrainian Mythology Based Game With Designs
 
原版定做(penn毕业证书)美国宾夕法尼亚大学毕业证文凭学历证书原版一模一样
原版定做(penn毕业证书)美国宾夕法尼亚大学毕业证文凭学历证书原版一模一样原版定做(penn毕业证书)美国宾夕法尼亚大学毕业证文凭学历证书原版一模一样
原版定做(penn毕业证书)美国宾夕法尼亚大学毕业证文凭学历证书原版一模一样
 
一比一原版(毕业证)长崎大学毕业证成绩单如何办理
一比一原版(毕业证)长崎大学毕业证成绩单如何办理一比一原版(毕业证)长崎大学毕业证成绩单如何办理
一比一原版(毕业证)长崎大学毕业证成绩单如何办理
 
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
一比一原版(NCL毕业证书)纽卡斯尔大学毕业证成绩单如何办理
 
一比一原版(UCB毕业证书)伯明翰大学学院毕业证成绩单如何办理
一比一原版(UCB毕业证书)伯明翰大学学院毕业证成绩单如何办理一比一原版(UCB毕业证书)伯明翰大学学院毕业证成绩单如何办理
一比一原版(UCB毕业证书)伯明翰大学学院毕业证成绩单如何办理
 
Connect Conference 2022: Passive House - Economic and Environmental Solution...
Connect Conference 2022: Passive House -  Economic and Environmental Solution...Connect Conference 2022: Passive House -  Economic and Environmental Solution...
Connect Conference 2022: Passive House - Economic and Environmental Solution...
 
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
一比一原版(LSE毕业证书)伦敦政治经济学院毕业证成绩单如何办理
 
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANEEASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
 
Technoblade The Legacy of a Minecraft Legend.
Technoblade The Legacy of a Minecraft Legend.Technoblade The Legacy of a Minecraft Legend.
Technoblade The Legacy of a Minecraft Legend.
 
一比一原版(Bristol毕业证书)布里斯托大学毕业证成绩单如何办理
一比一原版(Bristol毕业证书)布里斯托大学毕业证成绩单如何办理一比一原版(Bristol毕业证书)布里斯托大学毕业证成绩单如何办理
一比一原版(Bristol毕业证书)布里斯托大学毕业证成绩单如何办理
 
Expert Accessory Dwelling Unit (ADU) Drafting Services
Expert Accessory Dwelling Unit (ADU) Drafting ServicesExpert Accessory Dwelling Unit (ADU) Drafting Services
Expert Accessory Dwelling Unit (ADU) Drafting Services
 
一比一原版(Columbia毕业证)哥伦比亚大学毕业证如何办理
一比一原版(Columbia毕业证)哥伦比亚大学毕业证如何办理一比一原版(Columbia毕业证)哥伦比亚大学毕业证如何办理
一比一原版(Columbia毕业证)哥伦比亚大学毕业证如何办理
 
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
一比一原版(UAL毕业证书)伦敦艺术大学毕业证成绩单如何办理
 
一比一原版(UNUK毕业证书)诺丁汉大学毕业证如何办理
一比一原版(UNUK毕业证书)诺丁汉大学毕业证如何办理一比一原版(UNUK毕业证书)诺丁汉大学毕业证如何办理
一比一原版(UNUK毕业证书)诺丁汉大学毕业证如何办理
 
Research 20 slides Amelia gavryliuks.pdf
Research 20 slides Amelia gavryliuks.pdfResearch 20 slides Amelia gavryliuks.pdf
Research 20 slides Amelia gavryliuks.pdf
 
一比一原版(MMU毕业证书)曼彻斯特城市大学毕业证成绩单如何办理
一比一原版(MMU毕业证书)曼彻斯特城市大学毕业证成绩单如何办理一比一原版(MMU毕业证书)曼彻斯特城市大学毕业证成绩单如何办理
一比一原版(MMU毕业证书)曼彻斯特城市大学毕业证成绩单如何办理
 
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
一比一原版(Bolton毕业证书)博尔顿大学毕业证成绩单如何办理
 
projectreportnew-170307082323 nnnnnn(1).pdf
projectreportnew-170307082323 nnnnnn(1).pdfprojectreportnew-170307082323 nnnnnn(1).pdf
projectreportnew-170307082323 nnnnnn(1).pdf
 

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); }