Hi
Can anyone explain why the output of the following code:
#define A 1
#define B 2
A.B
after preprocessing only, is:
1 . 2
Why does the preprocessor put spaces between each character, when the
macro invocation has none?
The preprocessor is often -- and incorrectly -- said to
operate on the text of the program's source code. In fact,
it operates on tokens (formally, "preprocessing tokens")
that have been derived from the text. These tokens simply
come one after another, pretty maids all in a row; they do
not need to be separated by spaces. Equally, though, they
do not magically combine with each other just because they
happen to be adjacent.
So: By the time the sample code reaches the preprocessor,
it has been divided into three tokens which we may represent as
A
.
B
Macro substitution replaces A and B with other tokens -- one
apiece, in this case -- leading to
1
.
2
They are still three distinct and separate tokens, and do
not somehow collapse into a single 1.2 token.
Similarly,
#define S /
#define A *
S* A/
.... is a syntax error, not a comment. Macro substitution
operates on already-scanned tokens, not on text that will
later be scanned into tokens.
As for the spaces you see, well, that's just a sort of
artifact introduced for the sake of convenience. Remember,
the preprocessor operates on tokens and not on text. What
you see is the compiler's attempt to present these tokens in
a readable form. But this form can only be an approximation,
because the process that converts source characters to tokens
is not always invertible. That is one of the reasons the
Standard does not specify what the preprocessor's output looks
like, nor even that the output must be available outside the
compiler itself.