C Programming

C Programming

Variable Number of Arguments has 26 Questions

What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void fun(char *msg, ...);

int main()
{
    fun("ASTIWZ", 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);
}

ASTIWZ 1 7 11 0
1
4
7
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);
}

Error: fun() needs return type
Error: ptr Lvalue required
Error: Invalid declaration of fun(...)
No error
The macro va_arg is used to extract an argument from the variable argument list and advance the pointer to the next argument.

True
False
Can we pass a variable argument list to a function at run-time?

Yes
No
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 ", 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 10 3.14
A 10 3.14
A 10 10 3.140000 Hello
A 10 10 3.140000 Hello
A 10 Hello
A 10 Hello
Error
Point out the error if any in the following program (Turbo C).
#include<stdio.h>
#include<stdarg.h>
void display(int num, ...);

int main()
{
    display(4, 'A', 'a', 'b', 'c');
    return 0;
}
void display(int num, ...)
{
    char c; int j;
    va_list ptr;
    va_start(ptr, num);
    for(j=1; j<=num; j++)
    {
        c = va_arg(ptr, char);
        printf("%c", c);
    }
}

Error: unknown variable ptr
Error: Lvalue required for parameter
No error and print A a b c
No error and print 4 A a b c
In a function that receives variable number of arguments the fixed arguments passed to the function can be at the end of argument list.

True
False
While defining a variable argument list function we drop the ellipsis(...)?

Yes
No
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("");
}

2 4
3 6
2 4 8
3, 6, 9, 7
4 8
6 9 7
1 1 1
1 1 1 1
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>
void varfun(int n, ...);

int main()
{
    varfun(3, 7, -11, 0);
    return 0;
}
void varfun(int n, ...)
{
    va_list ptr;
    int num;
    num = va_arg(ptr, int);
    printf("%d", num);
}

Error: ptr has to be set at begining
Error: ptr must be type of va_list
Error: invalid access to list member
No error
A function that receives variable number of arguments should use va_arg() to extract arguments from the variable argument list.

True
False
Is it necessary that in a function which accepts variable argument list there should be at least be one fixed argument?

Yes
No
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", c1);
    }
}

A, A
B, B
C, C
D, D
A, a
B, b
C, c
D, d
A, 65
B, 66
C, 67
D, 68
A, 0
B, 0
C, 0
C, 0
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>

int main()
{
    void display(char *s, int num1, int num2, ...);
    display("Hello", 4, 2, 12.5, 13.5, 14.5, 44.0);
    return 0;
}
void display(char *s, int num1, int num2, ...)
{
    double c;
    char s;
    va_list ptr;
    va_start(ptr, s);
    c = va_arg(ptr, double);
    printf("%f", c);
    }
}

Error: invalid arguments in function display()
Error: too many parameters
Error: in va_start(ptr, s);
No error
For a function receives variable number of arguments it is necessary that the function should receive at least one fixed argument.

True
False
Can we write a function that takes a variable argument list and passes the list to another function?

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

Dogs 12
Cats 14
Boys 13
Apple 12
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>

int main()
{
    void display(int num, ...);
    display(4, 12.5, 13.5, 14.5, 44.3);
    return 0;
}
void display(int num, ...)
{
    float c; int j;
    va_list ptr;
    va_start(ptr, num);
    for(j=1; j<=num; j++)
    {
        c = va_arg(ptr, float);
        printf("%f", c);
    }
}

Error: invalid va_list declaration
Error: var c data type mismatch
No error
No error and Nothing will print
A function that receives variable number of arguments should use va_arg() to extract the last argument from the variable argument list.

True
False
Can the fixed arguments passed to the function that accepts variable argument list, occur at the end?

Yes
No
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>
void display(char *s, ...);
void show(char *t, ...);

int main()
{
    display("Hello", 4, 12, 13, 14, 44);
    return 0;
}
void display(char *s, ...)
{
    show(s, ...);
}
void show(char *t, ...)
{
    int a;
    va_list ptr;
    va_start(ptr, s);
    a = va_arg(ptr, int);
    printf("%f", a);
}

Error: invalid function display() call
Error: invalid function show() call
No error
Error: Rvalue required for t
va_list is an array that holds information needed by va_arg and va_end

True
False
It is necessary to call the macro va_end if va_start is called in the function.

Yes
No
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>
void varfun(int n, ...);

int main()
{
    varfun(3, 7, -11.2, 0.66);
    return 0;
}
void varfun(int n, ...)
{
    float *ptr;
    int num;
    va_start(ptr, n);
    num = va_arg(ptr, int);
    printf("%d", num);
}

Error: too many parameters
Error: invalid access to list member
Error: ptr must be type of va_list
No error
The macro va_start is used to initialise a pointer to the beginning of the list of fixed arguments.

True
False
The macro va_arg is used to extract an argument from the fixed micro argument list and advance the pointer to the next argument.

Yes
No