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");
1
  • 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. Commented Sep 2, 2014 at 5:30

7 Answers 7

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");
Sign up to request clarification or add additional context in comments.

Comments

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");

2 Comments

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: 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.
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.

Comments

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.

Comments

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 */

Comments

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

Comments

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.

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.