C Programming - Variable Number of Arguments - Discussion


What will be the output of the program?

void fun1(int num, ...);
void fun2(int num, ...);

int main()
    fun1(1, "Apple", "Boys", "Cats", "Dogs");
    fun2(2, 12, 13, 14);
    return 0;
void fun1(int num, ...)
    char *str;
    va_list ptr;
    va_start(ptr, num);
    str = va_arg(ptr, char *);
    printf("%s ", str);
void fun2(int num, ...)
    va_list ptr;
    va_start(ptr, num);
    num = va_arg(ptr, int);
    printf("%d", num);

[A]. Dogs 12
[B]. Cats 14
[C]. Boys 13
[D]. Apple 12

Answer: Option D


No answer description available for this question.

Nivethi said: (Nov 30, 2010)  
How the ans comes?

Udaya said: (Dec 1, 2010)  
First function calls the zeroth argument and first integer 12 is called.

Revathi said: (Jul 6, 2011)  
I didn't understand.

Bablu said: (Aug 5, 2011)  
I too didn't understand.

Sujatha said: (Sep 3, 2011)  
In first function it call the string so apple is printed.

In second function it calls the 2nd argument i.e., 12.

Karan said: (Aug 11, 2012)  
First it will start to print from the char* type arguments from the first function list.

Then at second function it will go for the 2nd argument because first argument already taken in first function.

(If the third function would be like str = va_arg(ptr, char *); then it will print 'cat' because its third).

Chaz said: (Apr 13, 2013)  
As I couldn't find any precise explanations, so I decided to post one myself:

va_start() always points to the 1st parameter of the "Variable Argument List (...)". Hence, in both the cases we are getting the first parameter as the output, as va_arg() is retrieving the first argument.

Note: If va_arg() is called multiple times by any mechanism then it will simply advance the pointer in the Var. Arg. List. It even returns garbage if the list's bound is passed.

Pallavi said: (Jul 11, 2013)  
In 1st function it calls %s. So it is string using va_list ptr, in 2nd function va_list ptr calls %d and at 2nd pointer of the 2nd function.

Savithri said: (Jul 15, 2014)  
Can any one explain it properly?

Ashwini Srivastava said: (Jan 5, 2015)  
At 1st calling function called i.e. str = va_arg(ptr, char *);

It takes very 1st character pointer which starts after the num {void fun1(int num, ...);} and when the 2nd calling function called i.e. num = va_arg(ptr, int);

It takes the 1st argument of function after num {void fun2(int num, ...);}

Here va_start(ptr, num) change the location of calling function.

If you cannot call va_start function in definition fun1 then it show segmentation fault and if you cannot call in definition of fun2 then it take after 12 i.e 13.

So, you have to understand how va_start and va_arg function works.

Ram said: (Jul 25, 2015)  
I didn't understand. Please correct explanation.

Anonymous said: (Apr 5, 2017)  
Here, in void func 1,

va _start( ptr , num ) points to first argument in func 1 ie '1'
str=va_arg (ptr,char*) points to next argument in func 1 ie 'Apple' as there is no loops it directly prints Apple.

In void func2,

va_start( ptr,num) points to first argument in void func2 ie '2'
va_ arg(ptr,int) points to next argument in void func2 ie. '12' as there is no loops it directly prints '12' as output.
So our output is Apple 12.

Ajay said: (Aug 18, 2017)  
According to me,


ptr is a va_list variable.
ptr , 1
str copy Apple // ptr=1 so first string copy

Apple //print


num= (12+13)/2 // average of first two number
num =12

12 //print

The final answer is;

Saba said: (Sep 12, 2017)  
I didn't understand. Please explain.

Brandon said: (Sep 24, 2017)  
First arg array:

1, "Apple", "Boys", "Cats", "Dogs"

va _start( ptr , 1)

puts the start point at the first element (arg arrays start at 1, not 0), so the start point is the element "1"

va_arg (ptr,char*)

says, return the first element that is a char pointer, which is the second element "Apple*

Second arg array:
2, 12, 13, 14

va _start( ptr , 2)

means the starting point it the second element in the array, "12"

va_ arg(ptr,int)

Means return the first element that is type int from the starting point. Since the element the staring pointer is point to, it returns it which is "12"

The confusion comes from:
1) element 1 is the first element in the array, NOT the element at index 0.
2) the first element is used as both the starting element and counts as an element in the array, it is not "consumed".
3) va_ arg returns the first element which matches the given data type, from the starting point previously set by va_start.

Goroparthi said: (Jan 7, 2018)  
How the answer comes? Please explain.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.