Exercise: Variable Number Of Arguments
Questions for: Variable Number Of Arguments
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>
fun(...);
int main()
{
fun(3, 7, -11.2, 0.66);
return 0;
}
fun(...)
{
va_list ptr;
int num;
va_start(ptr, n);
num = va_arg(ptr, int);
printf("%d", num);
}
A:
Error: fun() needs return type
B:
Error: ptr Lvalue required
C:
Error: Invalid declaration of fun(...)
D:
No error
Answer: C
There is no fixed argument in the definition fun()
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);
}
A:
Dogs 12
B:
Cats 14
C:
Boys 13
D:
Apple 12
Answer: D
No answer description is available. Let's discuss.
Discuss About this Question.
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void display(int num, ...);
int main()
{
display(4, 'A', 'B', 'C', 'D');
return 0;
}
void display(int num, ...)
{
char c, c1; int j;
va_list ptr, ptr1;
va_start(ptr, num);
va_start(ptr1, num);
for(j=1; j<=num; j++)
{
c = va_arg(ptr, int);
printf("%c", c);
c1 = va_arg(ptr1, int);
printf("%d\n", c1);
}
}
A:
A, A
B, B
C, C
D, D
B, B
C, C
D, D
B:
A, a
B, b
C, c
D, d
B, b
C, c
D, d
C:
A, 65
B, 66
C, 67
D, 68
B, 66
C, 67
D, 68
D:
A, 0
B, 0
C, 0
C, 0
B, 0
C, 0
C, 0
Answer: C
No answer description is available. Let's discuss.
Discuss About this Question.
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void dumplist(int, ...);
int main()
{
dumplist(2, 4, 8);
dumplist(3, 6, 9, 7);
return 0;
}
void dumplist(int n, ...)
{
va_list p; int i;
va_start(p, n);
while(n-->0)
{
i = va_arg(p, int);
printf("%d", i);
}
va_end(p);
printf("\n");
}
A:
2 4
3 6
3 6
B:
2 4 8
3, 6, 9, 7
3, 6, 9, 7
C:
4 8
6 9 7
6 9 7
D:
1 1 1
1 1 1 1
1 1 1 1
Answer: C
No answer description is available. Let's discuss.
Discuss About this Question.
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
A 10 3.14
B:
A 10 10 3.140000 Hello
A 10 10 3.140000 Hello
A 10 10 3.140000 Hello
C:
A 10 Hello
A 10 Hello
A 10 Hello
D:
Error
Answer: B
No answer description is available. Let's discuss.
Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.