Variadic Fuction

In C programming, there are plenty of concepts involved such as threads, pointers, structures, Linked List, Functions and etc.
In all those concepts, Functions are playing an significant role.
Functions are the very basic thing that makes us to define some separate concept.
We will often use functions, if we need to execute particular definitions.
Many functions types are there such as:

1. Functions with no argument
2. Functions with arguments
3. Functions with constant functions 

These are some common functions that probably known by all the programmers.

There are some other special functions:
Why it is needed?

  • In all those above functions, while calling the function coder should be aware of the number of arguments and type of arguments and all.
  •  You may face some difficulties while you are changing the existing program with the type of arguments. 
  •  Even though, the arguments of a function can be infinite. Some cases the number of arguments the argument types will be changing frequently.
  • In order to overcome this problem, variadic function has come in to the picture.
  • what is the use:
  • In order to send the unknown type of arguments this function is mainly used. You can send any number of arguments and any type of arguments to this variadic functions.
  • How it works:
  • This Section explains how to declare variadic function,how to write them, and how to call those variadic function.
This function has its own entities such as
------------------------------------------------------------------------------------------------------------------------------------------------------------------1. The function definition is quite different from usual functions.
        Syntax:
              return_type function_name(no of arguments,...) 
        Ex,
              int vari_fun(int count,...)

               -> While calling the function you can define the number of parameters to the function can be defined by using the following function call syntax
       Syntax:
              function_name(no of parameter,parameter1,parameter2,parameter3,parameter4....)
       Ex,
            vari_fun (2, "Hi", "Hi");

-----------------------------------------------------------------------------------------------------------------------------------------------------------------
2. va_list-A structure which is used to create a variadic function list objects.


          Syntax:
                 va_list list_object_name;
           Ex,
               va_list list;

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.va_arg - To fetch the next list argument
         Syntax:
                 va_arg (list_object_name, int);
          Ex,
                va_arg (list, int);
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
4. To clean up the list
         Syntax:
                va_end (list_object_name);
        Ex,
             va_end (list);
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Example Program:

     #include <stdarg.h>
     #include <stdio.h>
     int vari_func(int count,...)
     {
       va_list list;
       int i, sum;
       va_start (list, count);         // Initialize the argument list.      
       sum = 0;
       for (i = 0; i < count; i++)
              sum += va_arg (list, int);    // Get the next argument value.
       va_end (list);      
       return sum;
     }
     
     int main (void)
     {
       printf ("%d\n", vari_func (3, 5, 5, 6));
     
       vari_fun (2, "Hi", "Hi");
     
       return 0;
     }


Comments

Popular Posts