Why pre-processor gives a space?
Asked Answered
N

5

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 ?

Naturalistic answered 6/3, 2010 at 7:35 Comment(1)
Maybe you should add the "abusing_the_c_preprocessor" tag? ;)Microscopic
G
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.

Goosefoot answered 6/3, 2010 at 7:41 Comment(3)
#define is the only preprocessing instruction here. /* is not a preprocessor instruction, but it's not clear what you're referring to.Prent
@Potatoswatter: The preprocessor is responsible for removing comments before passing the code to the compiler.Goosefoot
According to C99 5.1.1.2/3, comments are whitespace and are not preprocessing tokens.Prent
M
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.)

Munster answered 6/3, 2010 at 7:56 Comment(0)
A
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++ = ' ';
Alcaide answered 6/3, 2010 at 7:57 Comment(0)
G
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.

Goosefoot answered 6/3, 2010 at 7:41 Comment(3)
#define is the only preprocessing instruction here. /* is not a preprocessor instruction, but it's not clear what you're referring to.Prent
@Potatoswatter: The preprocessor is responsible for removing comments before passing the code to the compiler.Goosefoot
According to C99 5.1.1.2/3, comments are whitespace and are not preprocessing tokens.Prent
P
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.

Prent answered 6/3, 2010 at 7:41 Comment(0)
B
0

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

#if 0
...
#endif
Begone answered 6/3, 2010 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.