1

I want to print info only if _DEBUG is defined

#define DEBUG(y) y == true ? #define _DEBUG true  : #define _DEBUG false

#ifdef _DEBUG
#define Print(s)  printf(s); 
#endif

Getting Error:

error: '#' is not followed by a macro parameter

Any suggestion how to achieve this with pre-processor directives?

I intend to use it from my main as:

DEBUG(true);
Print("Inside main in debug mode");
Gaurav K
  • 2,864
  • 9
  • 39
  • 68
  • From my understaning, the preprocessor statements are evaluated before compiling. So the the preprocess would evaluate the #Define statements before applying any of your logic. @JohnZwinck has the same approach that I would take. – Dan Sep 02 '14 at 05:30

7 Answers7

5

You cannot redefine a MACRO at run-time. Neither you can have a #define inside of another #define, like you try in the first line of your code.

You can do something like this:

#ifdef _DEBUG
#define Print(s)  printf("%s", s)
#else
#define Print(s)  
#endif

And use it from your main as:

#define _DEBUG
Print("Inside main in debug mode");
#undef _DEBUG
Print("Inside main debug mode off");

If you really need to switch debug on and off at run-time, your can do something like this:

void PrintIf(BOOL dbg, char * msg)
{
   if (dbg)
   {
       printf("%s", msg)
   }
}

And use it like this

y = TRUE;
PrintIf(y,"Inside main in debug mode");
y = FALSE;
PrintIf(y,"Inside main debug mode off");
Community
  • 1
  • 1
Andrej Adamenko
  • 1,650
  • 15
  • 31
3

Try this:

#ifdef DEBUG
#define Print(s) printf("%s", s)
#else
#define Print(s)
#endif

Then:

#define DEBUG
Print("Inside main in debug mode");
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Unless I'm missing something here, this won't work. The `Print` macro will get defined at the time the first block of code (`#ifdef DEBUG`) is parsed, so calling `#define DEBUG` or `#undef DEBUG` before using the `Print` macro will have no effect. That is, what `Print` does will already have been determined by the time it reaches the `#define DEBUG` line. – Cookyt Sep 02 '14 at 05:35
  • @Cookyt: You're right, `#define DEBUG` will need to come before the #include of whatever defines `Print()`. Or else the check could be made into a "runtime" (but perhaps optimized out) one, without the preprocessor at all. – John Zwinck Sep 02 '14 at 06:10
1

I intend to use it from my main as:

DEBUG(y);
Print("Inside main in debug mode");

Sorry, but ifdef are compile time (not run-time). You could use a global bool and runtime checking to enable and disable debug.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

You can't create preprocessor statements with macros as you are trying to do; it doesn't work and isn't allowed. For conditional printing, see C #define macro for debug printing.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

The problem occurs here:

#define DEBUG(y) y == true ? #define _DEBUG true  : #define _DEBUG false

When introducing #defines, the definition # occurs at the beginning of the line, not later (although) preprocessors generally allow one or two line indents.) You need to rewrite your #define eliminating the ternary operator simply as:

#ifdef _DEBUG
#define Print(s)  printf(s); 
#endif

While you may extend you defines with macros, you often introduce additional errors. It is generally better to stick to wrapping your _DEBUG code simply in #ifdef statements:

#ifdef _DEBUG
    fprintf (stderr, "your error messages\n");  // using standard printf/fprintf instead of macros
    ...
#endif  /* _DEBUG */
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
1

Macros are substituted at preprocessing stage and

#define DEBUG(y) y == true ? #define _DEBUG true  : #define _DEBUG false

this statement will be evaluated at compile time.

Conditional operator (ternary operator) are evaluated at compile time. So you are getting this error and # operator must always be used at the beginning of the statement that is the second mistake you are doing.

You can better use it this way

#define DEBUG
printf ("true");
#else
printf ("false");

You can also define this macro dynamically by using the gcc option -D

gcc -D DEBUG filename.c -o outputFile
Adarsh
  • 883
  • 7
  • 18
0

The first line is incorrect:

#define DEBUG(y) y == true ? #define _DEBUG true  : #define _DEBUG false

You cannot use #define inside preprocessor directive (like another #define)

And it does not make sense, since preprocessing happens before the real compilation (so before run time, when your y has some value). Read the cpp preprocessor documentation. Recall that sometimes the preprocessor is even a different program (/lib/cpp) but is today the first phase of most C compilers.

You could ask for the preprocessed form of your source code (e.g. with gcc -C -E source.c > source.i if using GCC) and look at that form with a pager (less source.i) or your editor.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547