7

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 ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
abubacker
  • 4,638
  • 6
  • 32
  • 35

5 Answers5

6

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
  • 698,415
  • 94
  • 811
  • 1,216
5

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
  • 510,854
  • 105
  • 1,084
  • 1,005
3

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
  • 951,095
  • 183
  • 1,149
  • 1,285
3

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
  • 134,909
  • 25
  • 265
  • 421
0

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

#if 0
...
#endif
eyalm
  • 3,366
  • 19
  • 21