C Programming - Variable Number of Arguments - Discussion
|
|
|
|
Read more:"Everyone is wise until he speaks."
- (Proverb)
|
| 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");
}
|
| [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 B
Explanation:
No answer description available for this question.
|
|
Pooja said:
(Thu, Aug 5, 2010 01:47:29 PM)
|
|
| |
| Please describe how to solve this problem. |
|
Raj said:
(Sat, Jul 30, 2011 02:04:07 PM)
|
|
| |
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:
(Fri, Oct 21, 2011 04:36:10 PM)
|
|
| |
what does while loop condition indicates??
i.e. while(n-->0)
what this condition indicates???? |
|
Anil said:
(Tue, Jan 3, 2012 05:14:05 PM)
|
|
| |
| 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. |
|
|