C Programming - Variable Number of Arguments - Discussion

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);
}

[A]. A 10 3.14
A 10 3.14
[B]. A 10 10 3.140000 Hello
A 10 10 3.140000 Hello
[C]. A 10 Hello
A 10 Hello
[D]. Error

Answer: Option B

Explanation:

No answer description available for this question.

Nishant said: (Aug 28, 2011)  
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.

Ranjan said: (Aug 23, 2012)  
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.

Nitin said: (Aug 23, 2012)  
In main Function two statements:
p1=fun1;
p2=fun2;
aren't These ambiguous..
They should be like:
p1 = &fun1;
p2 = &fun2;

Dev said: (Mar 6, 2013)  
@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.

Angel said: (Apr 25, 2015)  
Thanks guys for your explanation.

Nandan said: (Jun 18, 2015)  
How it printed twice?

Ganesh said: (Jul 11, 2016)  
Thanks for the explanations.

Deepak said: (Jun 15, 2020)  
What is p1 and p2? and where it defined? Explain.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.