C Programming - Variable Number of Arguments - Discussion
Discussion Forum : Variable Number of Arguments - Find Output of Program (Q.No. 5)
5.
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
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);
}
Discussion:
16 comments Page 2 of 2.
Karan said:
1 decade ago
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).
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).
Sujatha said:
1 decade ago
In first function it call the string so apple is printed.
In second function it calls the 2nd argument i.e., 12.
In second function it calls the 2nd argument i.e., 12.
Bablu said:
1 decade ago
I too didn't understand.
Revathi said:
1 decade ago
I didn't understand.
Udaya said:
1 decade ago
First function calls the zeroth argument and first integer 12 is called.
Nivethi said:
1 decade ago
How the ans comes?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers