tags:

views:

136

answers:

5

I want to comment a line using the pre-processor:

#define open /##*

#define close */

main()
{
        open commented line close
}

when I do $gcc -E filename.c I expected

/* commented line */ 

but I got

/ * commented line */ 

so that the compiler shows an error

Why it is giving an unwanted space ?

+3  A: 

The preprocessor runs and produces code in a form that the C compiler can understand. It only processes your code once, so even if you could produce a /* with your #define, the compiler would see the /* and give you an error because it's not valid C code (it's a preprocessing instruction).

This doesn't seem like a very good thing to do.

Greg Hewgill
`#define` is the only preprocessing instruction here. `/*` is not a preprocessor instruction, but it's not clear what you're referring to.
Potatoswatter
@Potatoswatter: The preprocessor is responsible for removing comments before passing the code to the compiler.
Greg Hewgill
According to C99 5.1.1.2/3, comments are whitespace and are not preprocessing tokens.
Potatoswatter
+3  A: 

Because comments are replaced with spaces before (and only before) the preprocessor runs. If you paste together the characters / and * using the preprocessor, you get /* which is just a couple operators. Edit: such abuse of ## technically creates /* as a single token, which has undefined behavior. You can paste together > ## > or < %:%: :, although you shouldn't.

See §6.4.6 of C99 for what tokens you are allowed to construct and 6.10.3.3 for the catenation process.

Potatoswatter
+6  A: 

From the GNU C Preprocessor documentation:

However, two tokens that don't together form a valid token cannot be pasted together. For example, you cannot concatenate x with + in either order. If you try, the preprocessor issues a warning and emits the two tokens. Whether it puts white space between the tokens is undefined. It is common to find unnecessary uses of '##' in complex macros. If you get this warning, it is likely that you can simply remove the '##'.

In this case '*' and '/' do not form a valid C or C++ token. So they are emitted with a space between them.

(Aside: you are likely to get C compilation errors even if you do manage to insert "comments" into the output of the C preprocessor. There aren't supposed to be any comments there.)

Stephen C
+5  A: 

The error is because /* is not a valid token.

As explained from the CPP doc:

two tokens that don't together form a valid token cannot be pasted together. For example, you cannot concatenate x with + in either order.

You can get the error by pasting other nonsense stuff e.g. /##+ or +##-.


About the space, it is deliberately inserted to avoid creating a comment and mess up the rest. From the GCC source code:

    /* Avoid comment headers, since they are still processed in stage 3.
         It is simpler to insert a space here, rather than modifying the
         lexer to ignore comments in some circumstances.  Simply returning
         false doesn't work, since we want to clear the PASTE_LEFT flag.  */
      if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
        *end++ = ' ';
KennyTM
A: 

If you want to comment some code using pre-processor, use

#if 0
...
#endif
eyalm