Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you #define a comment in C?

I'm trying to do a debug system but it seems not to work.

What I wanted to accomplish is something like this:

#ifndef DEBUG     #define printd // #else     #define printd printf #endif 

Is there a way to do that? I have lots of debug messages and I won't like to do:

if (DEBUG)     printf(...)  code  if (DEBUG)     printf(...)  ... 
like image 294
Ben B. Avatar asked Nov 23 '09 18:11

Ben B.


1 Answers

No, you can't. Comments are removed from the code before any processing of preprocessing directives begin. For this reason you can't include comment into a macro.

Also, any attempts to "form" a comment later by using any macro trickery are not guaranteed to work. The compiler is not required to recognize "late" comments as comments.

The best way to implement what you want is to use macros with variable arguments in C99 (or, maybe, using the compiler extensions).

like image 97
AnT Avatar answered Oct 05 '22 07:10

AnT