C Programming - Variable Number of Arguments - Discussion

Discussion Forum : Variable Number of Arguments - Find Output of Program (Q.No. 1)
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);
}
IndiaBIX 1 7 11 0
1
4
7
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
34 comments Page 2 of 4.

Manjiri said:   1 decade ago
What va_arg(ptr,int) do?

Kowshik said:   9 years ago
How to work this?

Please anyone explain me clearly.

Pranoti said:   8 years ago
Good explanation @Shiv.

Suraj chavan said:   8 years ago
@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);
}

GUNA said:   6 years ago
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.

Satapure abhishek said:   4 years ago
It is the ellipse program it is used to send multiple number of argument.
fun("indiabix",1,4,7,11,0);//fun call.
va_list ptr;//ptr is pointer of va_list;
num=va_arg(ptr,int);//num is int type ;
So it print 4;

Enia said:   4 years ago
num = va_arg(ptr, int); // means num fetches the next element in the arguments that is of int type.

First time, it fetches the argument with 1 value. Neglecting the char * as it's not of type int
and second time, it fetches the argument with the 4 value.

Irshad said:   1 decade ago
#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

Riya said:   1 decade ago
can anyone explain this please......

Onkar tiwari said:   1 decade ago
If anyone can explain then please explain. I will grateful to you.


Post your comments here:

Your comments will be displayed after verification.