C Programming - Variable Number of Arguments - Discussion
Discussion Forum : Variable Number of Arguments - Find Output of Program (Q.No. 2)
2.
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void fun1(char, int, int *, float *, char *);
void fun2(char ch, ...);
void (*p1)(char, int, int *, float *, char *);
void (*p2)(char ch, ...);
int main()
{
char ch='A'; int i=10;
float f=3.14; char *p="Hello";
p1=fun1;
p2=fun2;
(*p1)(ch, i, &i, &f, p);
(*p2)(ch, i, &i, &f, p);
return 0;
}
void fun1(char ch, int i, int *pi, float *pf, char *p)
{
printf("%c %d %d %f %s \n", ch, i, *pi, *pf, p);
}
void fun2(char ch, ...)
{
int i, *pi; float *pf; char *p;
va_list list;
printf("%c ", ch);
va_start(list, ch);
i = va_arg(list, int);
printf("%d ", i);
pi = va_arg(list, int*);
printf("%d ", *pi);
pf = va_arg(list, float*);
printf("%f ", *pf);
p = va_arg(list, char *);
printf("%s", p);
}
Discussion:
8 comments Page 1 of 1.
Deepak said:
5 years ago
What is p1 and p2? and where it defined? Explain.
Ganesh said:
9 years ago
Thanks for the explanations.
Nandan said:
1 decade ago
How it printed twice?
Angel said:
1 decade ago
Thanks guys for your explanation.
Dev said:
1 decade ago
@Nitin.
You are correct but, not completely.
p1 = &fun1;
p2 = &fun2;
In the above two statements the & is optional.
Will work in both the ways.
You are correct but, not completely.
p1 = &fun1;
p2 = &fun2;
In the above two statements the & is optional.
Will work in both the ways.
Nitin said:
1 decade ago
In main Function two statements:
p1=fun1;
p2=fun2;
aren't These ambiguous..
They should be like:
p1 = &fun1;
p2 = &fun2;
p1=fun1;
p2=fun2;
aren't These ambiguous..
They should be like:
p1 = &fun1;
p2 = &fun2;
Ranjan said:
1 decade ago
All things are obvious ,as we know va_arg treat as traversing the array of arguments. In every call va_arg jump to the next argument. And as given declaration of data output is correspondingly OK.
Nishant said:
1 decade ago
Ch=A, i=10,
Then &i is passed which points to i and finally once again 10 is printed, then pf=3.14 is printed and at last the string HELLO.
Then &i is passed which points to i and finally once again 10 is printed, then pf=3.14 is printed and at last the string HELLO.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers