1

I have many existing printf statements that I want to disable via:

#define printf(...) {};

But I want to define a new myprintf statement that will still use the stdio printf. How do I do this?

joemooney
  • 1,467
  • 1
  • 13
  • 17

1 Answers1

4

Use:

#define myprintf (printf)

The parentheses will disable macro expansion.

#include <stdio.h>

#define printf(...) do {} while(0)
#define myprintf (printf)

int main() {
  printf("printf\n");
  myprintf("myprintf\n");
}

(Not that I would recommend #defining printf away in the first place...)

For an explanation of why I've used do {} while(0) instead of {}, see Proper C preprocessor macro no-op

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Fantastic...thanks! This is for debugging but I am interested in the dangers of #defining printf in the first place – joemooney Mar 13 '13 at 17:25
  • The space in the `#define` line, between `myprintf` and `(`, is very important. If you instead wrote `#define myprintf(printf)`, then `myprintf` would be a macro that takes one argument and expands to nothing. – Keith Thompson Mar 13 '13 at 17:29
  • I don't think you need the `do ... while (0)` in this case. `#define printf(...)` should work, as long as no `printf` calls are used as operands. Another option is `#define printf(...) 0` – Keith Thompson Mar 13 '13 at 17:31