C Programming - Variable Number of Arguments - Discussion
Discussion Forum : Variable Number of Arguments - Point Out Errors (Q.No. 2)
2.
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);
}
}
Discussion:
15 comments Page 1 of 2.
Rinku Sharma said:
1 decade ago
What is the data type of ptr?
Here it is given as va_list.
Here it is given as va_list.
Shalini said:
1 decade ago
@rinku
va_list is a short cut for variable list..its an user defined data type!
va_list is a short cut for variable list..its an user defined data type!
Nobody said:
1 decade ago
What is va_list and va_start ?
Anamika said:
1 decade ago
What is va_list and va_start?
Alok said:
1 decade ago
In C++ we have a scope to do it using inline functions but in C is doubt.
Gsurav said:
1 decade ago
Why it didn't print 4?
Santosh said:
1 decade ago
c = va_arg(ptr, char);
What does char indicate in this statement?
What does char indicate in this statement?
Aashi said:
1 decade ago
Please give explanation.
Smit Gandhi said:
1 decade ago
Q:- va_list ptr;
A:- ptr ^\' This is the object of type va_list with information about the additional arguments and their retrieval state. This object should be initialized by an initial call to va_start before the first call to va_arg.
Q:- display (4, 'A', 'a', 'b', 'c');
A:- ptr - 4.
'A', 'a', 'b', 'c' (type) ^\' This is a type name. This type name is used as the type of the expression, this macro expands to.
Q:- What is the return value of this display function?
A:- This macro returns the next additional argument as an expression of type type.
For an example :- First it start wit 'A' then 'a' and so on upto 'c'.
A:- ptr ^\' This is the object of type va_list with information about the additional arguments and their retrieval state. This object should be initialized by an initial call to va_start before the first call to va_arg.
Q:- display (4, 'A', 'a', 'b', 'c');
A:- ptr - 4.
'A', 'a', 'b', 'c' (type) ^\' This is a type name. This type name is used as the type of the expression, this macro expands to.
Q:- What is the return value of this display function?
A:- This macro returns the next additional argument as an expression of type type.
For an example :- First it start wit 'A' then 'a' and so on upto 'c'.
Noboby said:
1 decade ago
If I compiled thus program in C compiler, I am getting illegal instruction.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers