C Programming - Variable Number of Arguments - Discussion
Discussion Forum : Variable Number of Arguments - Find Output of Program (Q.No. 3)
3.
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
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);
while(n-->0)
{
i = va_arg(p, int);
printf("%d", i);
}
va_end(p);
printf("\n");
}
Discussion:
20 comments Page 1 of 2.
Prince Bansal said:
1 decade ago
Wants to know more. Read this code.
#include<stdio.h>
//Header file contains Macros
#include<stdarg.h>
//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
va_start(valist,num);
//Run untill all variables are visted once
for(i=1;i<=num;++i)
{
//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.
sum+=va_arg(valist,int);
}
return sum/num;
}
int main()
{
printf("\nAVG:%e",avg(5, 1,2,3,4));
return 0;
}
#include<stdio.h>
//Header file contains Macros
#include<stdarg.h>
//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
va_start(valist,num);
//Run untill all variables are visted once
for(i=1;i<=num;++i)
{
//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.
sum+=va_arg(valist,int);
}
return sum/num;
}
int main()
{
printf("\nAVG:%e",avg(5, 1,2,3,4));
return 0;
}
Vimal Dahduk said:
1 decade ago
@Niyati.
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.
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.
Vinayk patil said:
10 years ago
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.
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:
9 years ago
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.
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.
Raj said:
1 decade ago
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.
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.
Anon said:
1 decade ago
@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
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
Anonymous said:
1 decade ago
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.
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.
Rahul said:
1 decade ago
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.
@Raj well, ques2 also talks about the same concept, but here, va_arg doesn't seem to point to the next argument.
Anil said:
1 decade ago
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.
Prince Bhanwra said:
1 decade ago
what does while loop condition indicates??
i.e. while(n-->0)
what this condition indicates????
i.e. while(n-->0)
what this condition indicates????
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers