1

I have prints available something like this

PrintMe(("abc %d",a));

Printme defined like this

#define Printme(_X_) printf _X_

But now i need to map it with some print which takes variable arguments something like

#define Printme(format , args ....)   PrintVar(30,format,##args)

Printvar has single parentheses and Printme has double parentheses

How to map this

2
  • Is there a reason you're using preprocessor macros instead of a normal function? Commented Oct 30, 2014 at 2:58
  • 1
    See this post for details on defining debug macros in c stackoverflow.com/questions/1644868/…. Commented Oct 30, 2014 at 2:59

2 Answers 2

1

You can do this:

#define Printme(format, ...)   PrintVar(30, format, ##__VA_ARGS__)

Documentation (for GCC): https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html

Sign up to request clarification or add additional context in comments.

3 Comments

But Printme has a format like Printme(("abc")); Printvar is like PrintVar(30, "abc") since its legacy code i cant change format of Printme
@Gajanana: OK, I've updated my answer to use the special ## before __VA_ARGS__ which makes it work in the case where you have a format string and no additional arguments.
Problem is Printme ha double Parentheses and PrintVar has single. So its not working –
0

How about:

#define PRINTME2(fmt, ...) printvar(30, fmt, ##__VA_ARGS__)
#define PRINTME(_X_) PRINTME2 _X_

A complete example:

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

void printvar(int lvl, char * fmt, ...)
{
    printf("lvl %d:", lvl);                                                                         
    va_list argptr;
    va_start(argptr,fmt);
    vprintf(fmt, argptr);
    va_end(argptr);
}

#define PRINTME2(fmt, ...) printvar(30, fmt, ##__VA_ARGS__)
#define PRINTME(_X_) PRINTME2 _X_

int main(void)
{
    PRINTME(("I have %u apples\n", 3));
    return 0;
}

Output:

lvl 30:I have 3 apples

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.