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);
}
Discussion:
34 comments Page 3 of 4.
Abhilash bhaduri said:
1 decade ago
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?
Kiran said:
1 decade ago
@irshad::thanks for your explination.
AMITH said:
1 decade ago
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' );
It depends on the variable arguments ur passing to the function
Ex: fun("IndiaBIX", 'I','N','D','I','A' );
Pruthvi said:
1 decade ago
What does int in va_arg(ptr, int) indicates? and is there any case where we can use char over there?
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
#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
Saurabh said:
1 decade ago
I didn't get it ! Can anyone expalin in little more detail ?
Anjali said:
1 decade ago
Sir please explane how it comes.
Anshu said:
1 decade ago
Yes I also thinks what shiv z saying, dats correct.
Nita said:
1 decade ago
I am aggry with shiv.
Shiv said:
1 decade ago
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'.
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'.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers