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

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Gajukorse
  • 147
  • 2
  • 11
  • Is there a reason you're using preprocessor macros instead of a normal function? – ipavl Oct 30 '14 at 02:58
  • See this post for details on defining debug macros in c http://stackoverflow.com/questions/1644868/c-define-macro-for-debug-printing. – Paul Rooney Oct 30 '14 at 02:59

2 Answers2

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

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • But Printme has a format like Printme(("abc")); Printvar is like PrintVar(30, "abc") since its legacy code i cant change format of Printme – Gajukorse Oct 30 '14 at 03:19
  • @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. – John Zwinck Oct 30 '14 at 03:27
  • Problem is Printme ha double Parentheses and PrintVar has single. So its not working – – Gajukorse Oct 30 '14 at 05:38
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

n0p
  • 3,399
  • 2
  • 29
  • 50