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 2 of 2.
K.karthik raja said:
1 decade ago
Please clearly explain this problem?
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;
}
Harini said:
1 decade ago
Actually n-->0 means that,
n-- > 0 i.e. n is first decremented and is compared with 0.
n-- > 0 i.e. n is first decremented and is compared with 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.
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
Niyati said:
1 decade ago
Please can anyone clearly explain this problem.I am not able to understand this.please..
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????
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.
Pooja said:
2 decades ago
Please describe how to solve this problem.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers