Options:
(a) vararg.h
(b) stdlib.h
(c) stdio.h
(d) stdarg.h
2] What would be the output of the following program?
# include
# include
main()
{
fun("Nothing specific", 1,4,7,11,0);
}
fun(char *msg, ...)
{
int tot = 0;
va_list ptr;
int num;
va_start(ptr, msg);
num = va_org(ptr,int);
num = va_arg(ptr,int);
printf("\n%d",num);
}
3] Is it necessary that in a function which accepts variable argument list there should at least be one fixed argument?
< Yes / No >
4] Can the fixed arguments passed to the functio which accepts variable argument list occur at the end? < Yes / No >
5] Point out the error, if any, in the follwoing program.
# include
main()
{
varfun(3,7,-11,0);
}
varfun(intn, ...)
{
va_list ptr;
int num;
num = va_arg(ptr,int);
printf("\n %d",num);
}
6] Point out the error, if any, in the follwoing program.
# include
main()
{
varfun(3,7.5,-11.2,0.66);
}
varfun(int n, ... )
{
float * ptr;
int num;
va_start(ptr,n);
num = va_arg(ptr,int);
printf("\n %d",num);
}
7] Point out the error, if any, in the follwoing program.
# include
main()
{
fun(1,4,7,11,0);
}
fun( ... )
{
va_list ptr;
int num;
va_start(ptr,int);
num = va_arg(ptr,int);
printf("\n %d",num);
}
8] The macro va_start is used to initialise a pointer to the beginning of the list of fixed arguments. < True / False >
9] The macro va_arg is used to extract an argument from the variable argumetn list and advance the pointer to the next argument. < True / False >
10] Point out the error, if any, in the following program.
# include"stdarg.h"
main()
{
display(4,'A','a','b','c');
}
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); } } 11] Mention any variable argument list function that you have used and its prototype. 12] Point out the error, if any, in the follwoing program. # include"stdarg.h" main() { display(4,12.5,13.5,14.5,44.3); } 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("\n %f",c); } } 13] Point out the error, if any, in the follwoing program. # include"stdarg.h" main() { display("Hello",4,2,12.5,13.5,14.5,44); } display(char *s,int num1, int num2, ... ) { float c; int j; va_list ptr; va_start(ptr, num); c = va_arg(ptr,double); printf("\n %f",c); } 14] Can we pass a variable argumetn list to a function at run-time? < Yes / No >
15] While defining a variable argument list function can we drop the ellipsis. ( ... ) < Yes / No >
16] Can we write a function that takes a variable argumetn list and passes the list to another function (which takes a variable number of argumetns) < Yes / No>
17] Point out the error, if any, in the foll0wing program.
# include "stdarg.h"
main()
{
display("Hello",4,12,13,14,44);
}
display(char *s, ... )
{
show(s, ... )
}
show(char *t, ... )
{
va_list ptr;
int a;
va_start(ptr,t);
a = va_arg(ptr,int);
printf("%f",a);
}
18] How would you rewrite program 18.17 such that show() is able to handle variable argument list?
19] If I use the following printf() to proint a long int why I am not warned about the type mismatch?
printf("%d",num);
20] Can you write a function that works similar to 'printf()'?
21] How can a called function determine the number of arguments that have been passed to it?
22] Can there be at least some solution to determine the number of arguments passed to a variable argument list function?
23] Point out the error in the following program.
# include "stdarg.h"
main()
{
int (*p1)();
int (*p2)();
int fun1(),fun();
p1 = fun1();
p2 = fun2();
display("Bye",p1,p2);
}
display(char *s, ... )
{
int (*pp1)();
va_list ptr;
va_start(ptr,s);
pp1 = va_arg(ptr,int(*)());
(*pp1)();
pp2 = va_arg(ptr,int(*)());
(*pp2)();
}
fun1()
{
printf("Hello");
}
fun2()
{
printf("Hi");
}
24] How would you rectify the error in the program 18.23 above?
***********************************************************************
************************* ANSWERS *****************************
***********************************************************************
1] D
2] 4
3] Yes
4] No
5] Since 'ptr' has not been set at the beginning of the variable argument list using va_start it may be pointing anywhere and hence a call to va_arg would fetch a wrong integer.
6] Irrespective of the type of argument passed to a function receiving variable argument list, 'ptr' must be of the type va_list.
7] There is no fixed argument i n the definition of 'fun()'.
8] False
9] True
10] While extracting a 'char' argument using va_arg we should have used c = va_arg(ptr,int).
11] printf(const char *format, ... )
12] While extracting a float argrment using va_arg we should have used c = va_arg(ptr,double).
13] While setting the 'ptr' to the beginning of variable argument list we should have used va_start(ptr,num2).
14] No. Every actual argument list must be completely known at compile time. In that sense it is not truely a variable argument list.
15] Yes
16] Yes
17] The call to 'show()' is improper. This is not the way to pass variable argument list to a function.
18] # include"stdarg.h"
main()
{
display("Hello",4,12,13,14,44);
}
display(char *s, ... )
{
va_list ptr;
va_start(ptr,s);
show(s,ptr);
}
show(char *,va_list ptr1)
{
int a,n,i;
a = va_arg(ptr1,int);
for(i = 0; i < a; i++) { n = va_arg(ptr1,int); printf("\n %d",n); } } 19] When a function accepts a variable number of arguments, its prototype cannot provide any information about the number of arguments and type of those variable arguments. Hence the compiler cannot warn about the mismatches. The programmer must make sure that arguments match or must manually insert explicit typecasts. 20] # include
# include
main()
{
void myprintf(char *, ... )
char *convert(unsigned int,int);
int i = 65;
char str[] = "Intranets are here to stay ..";
myprintf("\n Message = %s, %d %x",str,i,i);
}
void myprintf(char *fmt, ... )
{
char *p;
int i;
unsigned j;
char *s;
va_list argp;
va_start(argp,fmt);
p = fmt;
for(p = fmt; *p != '\0\; p++)
{
if(*p != '%')
{
putchar (*p);
continue;
}
p++;
switch(*p)
{
case 'c':
i = va_arg(argp,int);
putchar(i);
break;
case 'd':
i = va_arg(argp,int);
if(i < 0)
{
i = -i;
purchar('-');
}
puts(cionvert(i,10));
break;
case 'o':
u = va_arg(argp,unsigned int);
puts(convert(u,8));
break;
case 's':
s = va_arg(argp,char *);
puts(s);
break;
case 'u':
u = va_arg(argp,unsigned int);
puts(convert(u,10));
break;
case 'x':
u = va_arg(argp,unsigned int);
puts(convert(u,16));
break;
case '%':
purchar('%');
break;
}
}
va_end(argp);
}
char *convert(unsigned int num, int base)
{
static char buff[33];
char *ptr;
ptr = &buff[sizeof(buff) - 1];
*ptr = '\0\;
do
{
*--ptr = "0123456789abcdef"[num % base];
num /= base;
}while(num != 0);
returnptr;
}
21] It cannot. Any function that takes a variable number of arguments must be able to determine from the arguments themselves how many of them there are. The printf() function, for example, does this by looking for format specifiers (%d etc.) in the format string. This is the reason why such functions fail badly if the format string does not match the argument list.
22] When the arguments passed are all of same type we can think of passing a sentinel value like -1 or 0 or a NULL pointer at the end of the variable argument list. Another way could be to explicitly pass the count of number of variable arguments.
23] 'va_arg' cannot extract the function pointer from the variable argument list in the manner tried here.
24] Use 'typedef' as shown below:
# include"stdarg.h"
main()
{
int(*p1)();
int(*p2)();
int fun1(),fun2();
p1 = fun1;
p2 = fun2;
display("Bye",p1,p2);
}
display(char *s, ... )
{
int (*pp1)(),(*pp2)();
va_list ptr;
typedef int(*funcptr)();
va_start(ptr,s);
pp1 = va_arg(ptr,funcptr);
(*pp1)();
pp2 = va_arg(ptr,funcptr);
(*pp2)();
}
fun1()
{
printf("\n Hello");
}
fun2()
{
printf("\n Hi");
}
No comments:
Post a Comment