Can you #define a comment in C?

c, c-preprocessor, debugging

Solution

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

Problem

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(...) ... ```

Original source

Related problems