Is this possible to write a variable argument macro like this:

#include <stdio.h>
#include <stdarg.h>

#define func(x, ...)\
{\
         va_list ap;\
         va_start(ap, x);\
         int y = va_arg(ap, int);\
         printf("y is %d\n",y);\
}\

int main()
{
        int x;
        func(5, 4);
        return 0;
}

I found an interview question which asked how to write a variable argument macro so I gave it a try. I am getting compile error though.

Recommended Answers

All 5 Replies

Don't do that even if you would. The macro is too large and will greatly increase the size of the final program. Just write a normal function that takes variable arguments.

But anyway what would be the question to the interview question? I found in some forum posting real interview questions (I guess it is asked by bloomberg which asks all sorts of peculiar c questions)

Did you look at this?

commented: Nice link thanks, really helped me out +5

But anyway what would be the question to the interview question? I found in some forum posting real interview questions (I guess it is asked by bloomberg which asks all sorts of peculiar c questions)

Oh I see -- its one of those questions someone asks to see if you really know your stuff.

Did you look at this?

Now that is the thing I have been looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.