SlideShare a Scribd company logo
R.Ramya
Variable argument functions
 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 are needed from this header file
 va_list
 va_start
 va_arg
 va_end
Variable arguments in C
 va_list
 Datatype to store the list of arguments
 Declared like any other variable
 Ex. va_list ap;
 va_start
 initializes ap variable to be used with the va_arg and
va_end macros
 Accepts two arguments
 va_list
 Name of the variable that directly precedes the ellipsis
 Ex: va_start(ap, count);
Variable arguments in C
 va_arg
 returns the next argument of whatever type it is told
 moves down to the next argument in the list
 Accepts two arguments
 va_list
 Variable argument type
 Ex. va_arg(ap, int); //returns next int value in the argument
 va_end
 Cleans up the variable argument list
 Ex. va_end(ap);
To find minimum of ‘n’ numbers
#include <stdarg.h>
#include <stdio.h>
int min(int arg_count, ...)
{
int i; int min, a;
va_list ap;
va_start(ap, arg_count);
min = 9999;
for (i = 1; i <= arg_count; i++)
if ((a = va_arg(ap, int)) < min)
min = a;
va_end(ap);
return min;
}
To find minimum of ‘n’ numbers (Contd)
// Driver code
int main()
{
int count = 5;
printf("Minimum value is %d",
min(count, 12, 67, 6, 7, 100));
return 0;
}
To find average of ‘n’ numbers
#include <stdio.h>
#include <stdarg.h>
double average ( int num, ... )
{
va_list ap;
double sum = 0;
va_start ( ap, num ); //initializes, store all values
int x;
for ( x = 0; x < num; x++ )
sum += va_arg ( ap, double );
va_end ( ap );
return sum/num;
}
int main()
{
printf( "%fn", average ( 3, 12.2, 22.3, 4.5 ));
printf("%fn", average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ));
return 0;
}

More Related Content

What's hot

Lecture 2. mte 407
Lecture 2. mte 407Lecture 2. mte 407
Lecture 2. mte 407
rumanatasnim415
 
Circular queues
Circular queuesCircular queues
Circular queues
Ssankett Negi
 
C programming function
C  programming functionC  programming function
C programming function
argusacademy
 
Pointer to function 2
Pointer to function 2Pointer to function 2
Pointer to function 2
Abu Bakr Ramadan
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
Aditya Nihal Kumar Singh
 
Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3
Synapseindiappsdevelopment
 
functions in C
functions in Cfunctions in C
functions in C
Mehwish Mehmood
 
MEngine Advanced
MEngine AdvancedMEngine Advanced
MEngine Advanced
euming
 
Exercice.docx
Exercice.docxExercice.docx
Exercice.docx
imane26
 
Understanding Javascript Engine to Code Better
Understanding Javascript Engine to Code BetterUnderstanding Javascript Engine to Code Better
Understanding Javascript Engine to Code Better
Ihsan Fauzi Rahman
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examplesmua99
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
Vaishnavee Sharma
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfixSenthil Kumar
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
Abu Bakr Ramadan
 
Functions in c
Functions in cFunctions in c
Functions in c
Innovative
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
Vishalini Mugunen
 
Lecture4
Lecture4Lecture4
Lecture4
Rudy Martinez
 

What's hot (20)

Lecture 2. mte 407
Lecture 2. mte 407Lecture 2. mte 407
Lecture 2. mte 407
 
Circular queues
Circular queuesCircular queues
Circular queues
 
C programming function
C  programming functionC  programming function
C programming function
 
Queue oop
Queue   oopQueue   oop
Queue oop
 
Pointer to function 2
Pointer to function 2Pointer to function 2
Pointer to function 2
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3
 
functions in C
functions in Cfunctions in C
functions in C
 
MEngine Advanced
MEngine AdvancedMEngine Advanced
MEngine Advanced
 
Exercice.docx
Exercice.docxExercice.docx
Exercice.docx
 
Understanding Javascript Engine to Code Better
Understanding Javascript Engine to Code BetterUnderstanding Javascript Engine to Code Better
Understanding Javascript Engine to Code Better
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Lecture4
Lecture4Lecture4
Lecture4
 

Similar to Variadic functions

U4.ppt
U4.pptU4.ppt
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
Icaii Infotech
 
Tut1
Tut1Tut1
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
Nishant Munjal
 
Array Cont
Array ContArray Cont
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
rajatryadav22
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 
functionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdffunctionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdf
mounikanarra3
 
Chapter 13.1.7
Chapter 13.1.7Chapter 13.1.7
Chapter 13.1.7patcha535
 
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
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
JAYA
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
Tia Ricci
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointers
Rai University
 
Mca 2nd sem u-5 files & pointers
Mca 2nd  sem u-5 files & pointersMca 2nd  sem u-5 files & pointers
Mca 2nd sem u-5 files & pointers
Rai University
 

Similar to Variadic functions (20)

Variadic functions
Variadic functionsVariadic functions
Variadic functions
 
U4.ppt
U4.pptU4.ppt
U4.ppt
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
Tut1
Tut1Tut1
Tut1
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 
Array Cont
Array ContArray Cont
Array Cont
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
functionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdffunctionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdf
 
Chapter 13.1.7
Chapter 13.1.7Chapter 13.1.7
Chapter 13.1.7
 
Functions in C
Functions in CFunctions in C
Functions in C
 
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
 
Input And Output
 Input And Output Input And Output
Input And Output
 
Cd
CdCd
Cd
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointers
 
Mca 2nd sem u-5 files & pointers
Mca 2nd  sem u-5 files & pointersMca 2nd  sem u-5 files & pointers
Mca 2nd sem u-5 files & pointers
 

More from ramyaranjith

Tools for Project Excellence.pptx
Tools for Project Excellence.pptxTools for Project Excellence.pptx
Tools for Project Excellence.pptx
ramyaranjith
 
ML with IoT
ML with IoTML with IoT
ML with IoT
ramyaranjith
 
Regression
RegressionRegression
Regression
ramyaranjith
 
Bit fields
Bit fieldsBit fields
Bit fields
ramyaranjith
 
MongoDB - Features and Operations
MongoDB - Features and OperationsMongoDB - Features and Operations
MongoDB - Features and Operations
ramyaranjith
 
Unified Process
Unified Process Unified Process
Unified Process
ramyaranjith
 
CS6401 Operating systems - Solved Examples
CS6401 Operating systems - Solved ExamplesCS6401 Operating systems - Solved Examples
CS6401 Operating systems - Solved Examples
ramyaranjith
 

More from ramyaranjith (7)

Tools for Project Excellence.pptx
Tools for Project Excellence.pptxTools for Project Excellence.pptx
Tools for Project Excellence.pptx
 
ML with IoT
ML with IoTML with IoT
ML with IoT
 
Regression
RegressionRegression
Regression
 
Bit fields
Bit fieldsBit fields
Bit fields
 
MongoDB - Features and Operations
MongoDB - Features and OperationsMongoDB - Features and Operations
MongoDB - Features and Operations
 
Unified Process
Unified Process Unified Process
Unified Process
 
CS6401 Operating systems - Solved Examples
CS6401 Operating systems - Solved ExamplesCS6401 Operating systems - Solved Examples
CS6401 Operating systems - Solved Examples
 

Recently uploaded

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
JezreelCabil2
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
AG2 Design
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 

Recently uploaded (20)

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 

Variadic functions

  • 2. Variable argument functions  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
  • 3. Creating a Variadic Function  Use standard header - stdarg.h  4 macros are needed from this header file  va_list  va_start  va_arg  va_end
  • 4. Variable arguments in C  va_list  Datatype to store the list of arguments  Declared like any other variable  Ex. va_list ap;  va_start  initializes ap variable to be used with the va_arg and va_end macros  Accepts two arguments  va_list  Name of the variable that directly precedes the ellipsis  Ex: va_start(ap, count);
  • 5. Variable arguments in C  va_arg  returns the next argument of whatever type it is told  moves down to the next argument in the list  Accepts two arguments  va_list  Variable argument type  Ex. va_arg(ap, int); //returns next int value in the argument  va_end  Cleans up the variable argument list  Ex. va_end(ap);
  • 6. To find minimum of ‘n’ numbers #include <stdarg.h> #include <stdio.h> int min(int arg_count, ...) { int i; int min, a; va_list ap; va_start(ap, arg_count); min = 9999; for (i = 1; i <= arg_count; i++) if ((a = va_arg(ap, int)) < min) min = a; va_end(ap); return min; }
  • 7. To find minimum of ‘n’ numbers (Contd) // Driver code int main() { int count = 5; printf("Minimum value is %d", min(count, 12, 67, 6, 7, 100)); return 0; }
  • 8. To find average of ‘n’ numbers #include <stdio.h> #include <stdarg.h> double average ( int num, ... ) { va_list ap; double sum = 0; va_start ( ap, num ); //initializes, store all values int x; for ( x = 0; x < num; x++ ) sum += va_arg ( ap, double ); va_end ( ap ); return sum/num; } int main() { printf( "%fn", average ( 3, 12.2, 22.3, 4.5 )); printf("%fn", average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 )); return 0; }