T
T
Tolik2014-10-05 15:00:42
C++ / C#
Tolik, 2014-10-05 15:00:42

How to make a macro in C with an unlimited number of arguments that can be "disabled"?

I want all debug messages to be included if only one define is done. I have this:
DEBUG("program started\n");
and I have this:

#define DEBUG(str)                   \
#ifdef __DEBUG__                          \
  printf("DEBUG: " str);             \
#endif
and the program should only print "DEBUG: program started" if you add -D__DEBUG__ before the compiler options . But firstly, it doesn’t work, and secondly, it’s done only for 1 argument, but I would have for any number.
error: '#' is not followed by a macro parameter
UPD:
It works if so:
#define DEBUG(...)                        \
printf("DEBUG: " __VA_ARGS__);
but I need to disable the debugger. For this he did this:
#ifdef __DEBUG__
#define DEBUG(...) printf("DEBUG: " __VA_ARGS__);
#endif
but here if you do not define __DEBUG__ then I get an error because DEBUG is undefined. How then, otherwise, can DEBUG be defined so that it doesn't do anything?
#ifdef __DEBUG__
  #define DEBUG(...) printf("DEBUG: " __VA_ARGS__);
#else
  // Что тут писать чтобы объявить DEBUG() который ничего не делает?
#endif

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mamkaololosha, 2014-10-05
@Diel

#ifdef __DEBUG__
  #define DEBUG(...) printf("DEBUG: " __VA_ARGS__);
#else
  #define DEBUG(...)
#endif

well, how else?

A
Alexander Taratin, 2014-10-05
@Taraflex

http://www.google.com/search?q=c+variadic+macro&oq...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question