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);
    }
}
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
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
15 comments Page 2 of 2.

Arunmane said:   10 years ago
I think, it is undefined behaviour. Because in the va_arg will return character means, char can not be copied into char variable.

Example: Char c = 'A'; // this is correct way.

But in the va_arg will return char value not its ASCII, so it will be like this.

Char c = A // This is wrong. So it is undefined behaviour.

Robert said:   9 years ago
Guys, watch out for some warning that will give runtime error with GCC compiler:

\Untitled1.c|13|warning: 'char' is promoted to 'int' when passed through '...'|
\\Untitled1.c|13|note: (so you should pass 'int' not 'char' to 'va_arg')|
\Untitled1.c|13|note: if this code is reached, the program will abort|

Normally the remedy for this should be to pass instead of : c = va_arg(ptr, char)

c = va_arg(ptr, int);

Pooja said:   8 years ago
What is va_start?

Sowjanya said:   8 years ago
Please give clear explanation about this question.

V.Nandhini said:   6 years ago
I am not understanding it. Please, anyone, help me to get it.


Post your comments here:

Your comments will be displayed after verification.