top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

    Get Free Article Updates

Facebook Login
Site Registration

Variable Argument List in C

0 votes
156 views

Perhaps you would like to have a function that will accept any number of values and then return the average. You don't know how many arguments will be passed in to the function. One way you could make the function would be to accept a pointer to an array. Another way would be to write a function that can take any number of arguments. So you could write avg(4, 12.2, 23.3, 33.3, 12.1); or you could write avg(2, 2.3, 34.4); The advantage of this approach is that it's much easier to change the code if you want to change the number of arguments. Indeed, some library functions can accept a variable list of arguments (such as printf--I bet you've been wondering how that works!).

Whenever a function is declared to have an indeterminate number of arguments, in place of the last argument you should place an ellipsis (which looks like '...'), so, int a_function ( int x, ... ); would tell the compiler the function should accept however many arguments that the programmer uses, as long as it is equal to at least one, the one being the first, x.

We'll need to use some macros (which work much like functions, and you can treat them as such) from the stdarg.h header file to extract the values stored in the variable argument list -- va_start, which initializes the list, va_arg, which returns the next argument in the list, and va_end, which cleans up the variable argument list.

To use these functions, we need a variable capable of storing a variable-length argument list--this variable will be of type va_list. va_list is like any other type.

For example, the following code declares a list that can be used to store a variable number of arguments.
va_list a_list;

va_start is a macro which accepts two arguments, a va_list and the name of the variable that directly precedes the ellipsis ("..."). So in the function a_function, to initialize a_list with va_start, you would write va_start ( a_list, x );

int a_function ( int x, ... )
{
    va_list a_list;
    va_start( a_list, x );
}

To show how each of the parts works, take an example function:

#include <stdarg.h>
#include <stdio.h>

double average (int num, ... )
{
    va_list arguments;                     
    double sum = 0;

    /* Initializing arguments to store all values after num */
    va_start (arguments, num);           

    /* Sum all the inputs; we still rely on the function caller to tell us how
     * many there are */
    for (int x = 0; x < num; x++)        
    {
        sum += va_arg (arguments, double); 
    }

    va_end (arguments);                  // Cleans up the list
    return sum / num;                      
}

int main()
{
    /* this computes the average of 3 values */
    printf( "%f\n", average (3, 12.2, 22.3, 4.5));

    /* here it computes the average of the 5 values */
    printf( "%f\n", average (5, 3.3, 2.2, 1.1, 5.5, 3.3));
}
posted May 20, 2014 by Atul Mishra

  Promote This Article
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button


Related Articles

The easiest way to find out what min or max value a number that a particular type can hold is to use the values defined in the ANSI standard header file limits.h. This file contains many useful constants defining the values that can be held by various types, including these:

Value           Description
CHAR_BIT    -   Number of bits in a char
CHAR_MAX    -   Maximum decimal integer value of a char
CHAR_MIN    -   Minimum decimal integer value of a char
MB_LEN_MAX  -   Maximum number of bytes in a multibyte character
INT_MAX     -   Maximum decimal value of an int
INT_MIN     -   Minimum decimal value of an int
LONG_MAX    -   Maximum decimal value of a long
LONG_MIN    -   Minimum decimal value of a long
SCHAR_MAX   -   Maximum decimal integer value of a signed char
SCHAR_MIN   -   Minimum decimal integer value of a signed char
SHRT_MAX    -   Maximum decimal value of a short
SHRT_MIN    -   Minimum decimal value of a short
UCHAR_MAX   -   Maximum decimal integer value of unsigned char
UINT_MAX    -   Maximum decimal value of an unsigned integer
ULONG_MAX   -   Maximum decimal value of an unsigned long int
USHRT_MAX   -   Maximum decimal value of an unsigned short int

For integral types, on a machine that uses two's complement arithmetic (which is just about any machine you're likely to use), a signed type can hold numbers from [-2^(number of bits - 1)] to [+2^(number of bits - 1) - 1].

READ MORE
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...