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.

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

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

Supriya said:   10 years ago
The C library macro type va_arg(va_list ap, type) retrieves the next argument in the parameter list of the function with type.

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?

Raman kumar said:   1 decade ago
I think num = va_arg(ptr, int);

Keep the value 6 in num and this statement num = va_arg(ptr, int); hold 1 as it is true.

Yogeshwar Singh said:   1 decade ago
What is the meaning of attach ptr with pointer and why we need to do so ?

va_start(ptr, msg); /* attach ptr with msg */

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?

Praveen said:   1 decade ago
Sir I think va_start (ptr, msg) should hold 6.

And num must return 6. ?

Give me region how it work.

Arun said:   1 decade ago
Can anyone tell me application of multi argument function, real world application?

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.