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.

Sri lakshmi said:   1 decade ago
What is va_arg() do?

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.

ALEX said:   1 decade ago
Why we use ... (only 3 dots) for variable arguments?

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 */

Lavanya said:   1 decade ago
#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; /* create a new list ptr */
int num;
va_start(ptr, msg); /* attach ptr with msg */
num = va_arg(ptr, int); /* fetch 2nd argument */
num = va_arg(ptr, int); /* fetch 3rd argument */
printf("%d", num);
}

Akash said:   1 decade ago
What does it mean va_start(ptr, msg);?

Manjiri said:   1 decade ago
What va_arg(ptr,int) do?

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

Avijit_softlove said:   1 decade ago
#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; /* create a new list ptr */
int num;
va_start(ptr, msg); /* attach ptr with msg */
num = va_arg(ptr, int); /* fetch 2nd argument */
num = va_arg(ptr, int); /* fetch 3rd argument */
printf("%d", num);
}

Akash Kharade said:   1 decade ago
Thanx friends.....now i got it.


Post your comments here:

Your comments will be displayed after verification.