

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Count Variable Numbers of Arguments in C?
In this section we will see how to count number of arguments in case of variable number of arguments in C.
The C supports ellipsis. This is used to take variable number of arguments to a function. User can count the arguments by using one of the three different ways.
By passing the first argument as count of the parameters
By passing last argument as NULL.
Using the logic like printf() or scanf() where the first argument has the placeholders for other arguments.
In the following program, we will total number of variable of arguments passed.
Example Code
#include<stdio.h> #include <stdarg.h> int get_avg(int count, ...) { va_list ap; int i; int sum = 0; va_start(ap, count); //va_start used to start before accessing arguments for(i = 0; i < count; i++) { sum += va_arg(ap, int); } va_end(ap); //va_end used after completing access of arguments return sum; } main() { printf("Total variable count is: %f", get_avg(5, 8, 5, 3, 4, 6)); }
Output
Total variable count is: 5
- Related Questions & Answers
- Variable number of arguments in C++
- Variable Arguments (Varargs) in C#
- Variable length arguments for Macros in C
- How to use variable number of arguments to function in JavaScript?
- Variable number of arguments in Lua Programming
- Variable-length arguments in Python
- Demonstrating variable-length arguments in Java
- What are variable arguments in java?
- How to use variable-length arguments in a function in Python?
- Command Line and Variable Arguments in Python?
- Count of Smaller Numbers After Self in C++
- How to swap two numbers without using a temp variable in C#
- What are the arguments to Tkinter variable trace method callbacks?
- Count common prime factors of two numbers in C++
- How to Parse Command Line Arguments in C++?
Advertisements