Question

show how we can use variable-length arguments and explain why we may want to use variable-length...

show how we can use variable-length arguments and explain why we may want to use variable-length arguments in our programs.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Variable Length Argument in C

Variable length argument is a feature that allows a function to receive any number of arguments. There are situations where we want a function to handle variable number of arguments according to requirement.
1) Sum of given numbers.
2) Minimum of given numbers.
and many more.

Variable number of arguments are represented by three dotes (…)

Below is an example, to find minimum of given set of integers

// C program to demonstrate use of variable

// number of arguments.

#include <stdarg.h>

#include <stdio.h>

  

// this function returns minimum of integer

// numbers passed. First argument is count

// of numbers.

int min(int arg_count, ...)

{

    int i;

    int min, a;

  

    // va_list is a type to hold information about

    // variable arguments

    va_list ap;

  

    // va_start must be called before accessing

    // variable argument list

    va_start(ap, arg_count);

  

    // Now arguments can be accessed one by one

    // using va_arg macro. Initialize min as first

    // argument in list

    min = va_arg(ap, int);

  

    // traverse rest of the arguments to find out minimum

    for (i = 2; i <= arg_count; i++)

        if ((a = va_arg(ap, int)) < min)

            min = a;

  

    // va_end should be executed before the function

    // returns whenever va_start has been previously

    // used in that function

    va_end(ap);

  

    return min;

}

  

// Driver code

int main()

{

    int count = 5;

    printf("Minimum value is %d", min(count, 12, 67, 6, 7, 100));

    return 0;

}

Here we use macros to implement the functionality of variable arguments.

  • Use va_list type variable in the function definition.
    int a_function(int x, ...)
    {
        va_list a_list;
        va_start( a_list, x );
    }
  • Use int parameter and va_start macro to initialize the va_list variable to an argument list. The macro va_start is defined in stdarg.h header file.
  • Use va_arg macro and va_list variable to access each item in argument list.
  • macro va_end to clean up the memory assigned to va_list variable.

What is variable length arguments (var-args)?

In programming, there happens situation when you want your function to accept variable number of arguments. For example - suppose I ask you to write a function to find maximum. You will end up with function declaration similar to

int maximum(int n1, int n2, int n3); // Find maximum between three numbers
OR
int maximum(int n1, int n2, int n3, int n4, int n5); // Find maximum between five numbers

However, none of the above declaration is suitable for the case. First function will find maximum between three numbers likewise second will find maximum of five numbers. What if I need to find maximum between four, ten or sometime n numbers. For such case, we use variable length arguments in a function.

Variable length arguments is a programming construct that allows programmers to pass n number of arguments to a function. You can also call variable length argument as var-args.

Add a comment
Know the answer?
Add Answer to:
show how we can use variable-length arguments and explain why we may want to use variable-length...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT