C Programming - Variable Number of Arguments - Discussion


What will be the output of the program?

void dumplist(int, ...);

int main()
    dumplist(2, 4, 8);
    dumplist(3, 6, 9, 7);
    return 0;
void dumplist(int n, ...)
    va_list p; int i;
    va_start(p, n);

        i = va_arg(p, int);
        printf("%d", i);

[A]. 2 4
3 6
[B]. 2 4 8
3, 6, 9, 7
[C]. 4 8
6 9 7
[D]. 1 1 1
1 1 1 1

Answer: Option C


No answer description available for this question.

Pooja said: (Aug 5, 2010)  
Please describe how to solve this problem.

Raj said: (Jul 30, 2011)  
First dumplist function is called with elements 2, 4 and 8. When va_start is called, it points to element 2.

When it sees va_arg, it points to the next element 4.

Since va_arg is within while loop, it prints 4 8 for the first function and 6 9 7 for the second function.

So the answer is option C.

Prince Bhanwra said: (Oct 21, 2011)  
what does while loop condition indicates??

i.e. while(n-->0)

what this condition indicates????

Anil said: (Jan 3, 2012)  
while(n-- >0) ...loop until the value of n is greater than 0. I think here n represents the no of arguments passed excluding n.

Niyati said: (Feb 18, 2012)  
Please can anyone clearly explain this problem.I am not able to understand this.please..

Anon said: (Aug 19, 2012)  
@Raj... When it sees va_arg, it points to the next element 4.

Since va_arg is within while loop, it prints 4 8 for the first function and 6 9 7 for the second function.What you are saying here is incorrect.va_arg returns the element it is pointing to and the move to next element in list

Vimal Dahduk said: (Nov 12, 2012)  

while(n-- > 0) means u understand so n is indicate total number of argument.

va_start is indicate first argument. va_args(p,int) return first argument value and va_args(p,int) second argument value.

so that print first function and second function and not print first value.

Print first value by simply write printf("%d", n); of the above example.

I hope you understand.

Harini said: (Mar 22, 2014)  
Actually n-->0 means that,

n-- > 0 i.e. n is first decremented and is compared with 0.

Prince Bansal said: (Apr 17, 2014)  
Wants to know more. Read this code.

//Header file contains Macros
//Compute avg of given no's
double avg(int num, ...)
int i;
double sum=0.0;
//Create va_list variable
va_list valist;
//Initializa va_list with num
//Run untill all variables are visted once
//va_arg will pick up variable from argument list and each
//time move 4 bytes ahead (because we are using here integer
//second argument in va_arg.
return sum/num;
int main()
printf("\nAVG:%e",avg(5, 1,2,3,4));
return 0;

K.karthik Raja said: (Jul 1, 2014)  
Please clearly explain this problem?

Rahul said: (Jul 11, 2014)  
Can anyone please explain this in detail.

@Raj well, ques2 also talks about the same concept, but here, va_arg doesn't seem to point to the next argument.

Priya said: (Sep 19, 2015)  
I can't understand, please anyone explain the above program clearly.

Kalpana said: (Sep 25, 2015)  
I need a clear explanation about this program.

Anonymous said: (Oct 1, 2015)  
Here first variable passed in both calls will be considered as the starting point to work with variable list.

In first part,

va start : 2.

So n would be, 2 so loop runs for 4 and 7.

Whereas in second case, va start:3 so 6 9 and 7 is printed on the screen.

Vinayk Patil said: (Nov 7, 2015)  
In both dumplist 2, 3 initial values represent no of arguments taken remaining are values stored so n->0 indicates for 1st while (n->0) indicates n is equal to 0 for both times of dumplist.

It is checked but initial value of n=2 no of arguments taken by dumplist accordingly n=3 for 2nd one n--is done according to printf function respective values included initially is printed.

Vvvvvvv said: (Jul 21, 2016)  
In the list, the first parameter always represents the number of arguments to be passed.

So the 2 in the first function represents that there are 2 arguments.

And 3 in second function represents there are 3 arguments.

So first it prints 4 and 8.

Next, it will print 6, 9, 7. Excluding 4 and 8.
4 and 8 are actually to define a number of parameters. And they are not get printed.

Sandipan said: (Oct 6, 2016)  
What is va_end?

Teju said: (Mar 30, 2017)  
while(n-->0) Explain this line.

Mahi said: (Jun 17, 2017)  
Decrement value of n.

Untill (n--) to 0.

Akshaya Shelke said: (Jul 7, 2017)  
Thanks for your explanation @Vvvvvvv.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.