C Programming - Variable Number of Arguments - Discussion

1. 

What will be the output of the program?

#include<stdio.h>
#include<stdarg.h>
void fun(char *msg, ...);

int main()
{
    fun("IndiaBIX", 1, 4, 7, 11, 0);
    return 0;
}
void fun(char *msg, ...)
{
    va_list ptr;
    int num;
    va_start(ptr, msg);
    num = va_arg(ptr, int);
    num = va_arg(ptr, int);
    printf("%d", num);
}

[A]. IndiaBIX 1 7 11 0
[B]. 1
[C]. 4
[D]. 7

Answer: Option C

Explanation:

No answer description available for this question.

Praveen said: (Nov 22, 2010)  
Sir I think va_start (ptr, msg) should hold 6.

And num must return 6. ?

Give me region how it work.

Riya said: (Dec 29, 2010)  
can anyone explain this please......

Onkar Tiwari said: (Jan 1, 2011)  
If anyone can explain then please explain. I will grateful to you.

Virat said: (Jan 18, 2011)  
Please explain this question.

Shiv said: (Jan 23, 2011)  
va_list ptr is an argument pointer, used to traverse through the variable arguments passed in the function.

va_start points 'ptr' to the first argument, in this case 'IndiaBix'

Every call to va_arg moves the ptr to next variable argument.

Hence after 2 calls to va_arg, the pointer ends up at '7' and num is '4'.

Nita said: (Feb 15, 2011)  
I am aggry with shiv.

Anshu said: (Mar 7, 2011)  
Yes I also thinks what shiv z saying, dats correct.

Anjali said: (Apr 5, 2011)  
Sir please explane how it comes.

Saurabh said: (May 15, 2011)  
I didn't get it ! Can anyone expalin in little more detail ?

Irshad said: (Jun 30, 2011)  
#include<stdio.h>
#include<stdarg.h>
void irshad(char *arr, ...)
{
int num;
va_list ptr; /* create a new list ptr */
va_start(ptr,arr); /* attach ptr with arr */
printf("\n num=%d",arr); /* it will print 1st argument */
num = va_arg(ptr,int); /* fetch 2nd argument */
printf("\n num=%d",num);
num = va_arg(ptr,int); /* fetch 3rd argument */
printf("\n num=%d",num);
}
int main()
{

printf("\n variable argument:");
irshad(10,20,30,40,50,0);
return 0;
}


/* output */

variable argument:
num=10
num=20
num=30


Please let me know if any problem to understand this one.
irshadalam.mca[at]gmail.com

Pruthvi said: (Sep 14, 2011)  
What does int in va_arg(ptr, int) indicates? and is there any case where we can use char over there?

Amith said: (Sep 28, 2011)  
We can also use va_arg(ptr, char);

It depends on the variable arguments ur passing to the function

Ex: fun("IndiaBIX", 'I','N','D','I','A' );

Kiran said: (Nov 18, 2011)  
@irshad::thanks for your explination.

Abhilash Bhaduri said: (Jan 23, 2012)  
In 1 place in random test you say that va_end should be present for every va_start. But here it compiles with no error. How?

Akash Kharade said: (Sep 16, 2012)  
Thanx friends.....now i got it.

Avijit_Softlove said: (Oct 9, 2012)  
#include<stdio.h>
#include<stdarg.h>
void fun(char *msg, ...);

int main()
{
fun("IndiaBIX", 1, 4, 7, 11, 0);
return 0;
}
void fun(char *msg, ...)
{
va_list ptr; /* create a new list ptr */
int num;
va_start(ptr, msg); /* attach ptr with msg */
num = va_arg(ptr, int); /* fetch 2nd argument */
num = va_arg(ptr, int); /* fetch 3rd argument */
printf("%d", num);
}

Arun said: (Aug 6, 2013)  
Can anyone tell me application of multi argument function, real world application?

Manjiri said: (Aug 29, 2013)  
What va_arg(ptr,int) do?

Akash said: (Oct 10, 2013)  
What does it mean va_start(ptr, msg);?

Lavanya said: (Mar 7, 2014)  
#include<stdio.h>
#include<stdarg.h>
void fun(char *msg, ...);

int main()
{
fun("IndiaBIX", 1, 4, 7, 11, 0);
return 0;
}
void fun(char *msg, ...)
{
va_list ptr; /* create a new list ptr */
int num;
va_start(ptr, msg); /* attach ptr with msg */
num = va_arg(ptr, int); /* fetch 2nd argument */
num = va_arg(ptr, int); /* fetch 3rd argument */
printf("%d", num);
}

Yogeshwar Singh said: (May 20, 2014)  
What is the meaning of attach ptr with pointer and why we need to do so ?

va_start(ptr, msg); /* attach ptr with msg */

Alex said: (Jun 8, 2014)  
Why we use ... (only 3 dots) for variable arguments?

Raman Kumar said: (Jul 25, 2014)  
I think num = va_arg(ptr, int);

Keep the value 6 in num and this statement num = va_arg(ptr, int); hold 1 as it is true.

Sri Lakshmi said: (Dec 16, 2014)  
What is va_arg() do?

Vandana said: (Dec 28, 2015)  
Va_list ptr;

Va_list is not struct name than why we are creating a new pointer of Va_list type.

What is this? Is any data type?

Is Va_start is builtin function?

I am very confused please tell me the every thing in a proper manner.

Supriya said: (Jan 25, 2016)  
The C library macro type va_arg(va_list ap, type) retrieves the next argument in the parameter list of the function with type.

Suhas said: (Nov 25, 2016)  
1. There are three macros available in file "stdarg.h" --->.

- va_start: is used to initialize the pointer to the beginning of the list of optional arguments.
- va_arg: is used to advance the pointer to next argument *.
- va_list: va_list is like any other type.

Kowshik said: (Feb 26, 2017)  
How to work this?

Please anyone explain me clearly.

Pranoti said: (Jun 4, 2017)  
Good explanation @Shiv.

Suraj Chavan said: (Mar 24, 2018)  
@All.

What was the answer to this code?

Please explain it with the answer.

#include<stdio.h>
Void main()
{
char CHAR='\328';
char CHAR1='\323'
printf("CHAR=%d\n CHAR1=%d\n",CHAR,CHAR1);
}

Manish Roy said: (Apr 2, 2019)  
First of all va_start and va_arg are a macro defined in stdarg.h header file.

Va_list type is also defined in the stdarg.h,
Here ptr is va_list type variable.

Va_start macro and int parameter is used to initialize the va_list variable to an argument list.
Then we use va_arg macro and va_list variable to access each item in an argument list.
So num=va_arg(ptr,int); assigns integer value to num i.e.1.
Then the second statement num=va_arg(ptr,int);assigns 4 to num.
Then printf statement is given so 4 is printed and hence the output.

Guna said: (May 26, 2019)  
Here, va_list, it is only focused to the defined values in the func arguments. As same here, here we have char msg also, that is totally differ on output. Main thing is on First call of var_arg it takes the first value of the argument and second time by default it moves on next value, that's what we got the output as 4.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.