Why is C/C++ preprocessor adding a space here?

I have a tiny problem with a preprocessor that puzzles me and I cannot find any explanation to it in the documentation/preprocessor/language spec.

#define booboo() aaa
booboo()bbb
booboo().bbb

is preprocessed into:

aaa bbb   <--- why is space added here
aaa.bbb

After handling trigraphs, continued lines and comments, preprocessor works on preprocessor directives and divides input into preprocessing tokens and whitespace. booboo’s replacement list comprises one pp-token which is identifier ‘aaa’. booboo()bbb is divided into pp-tokens: ‘booboo’, ‘(‘, ‘)’, ‘bbb’. Sequence of ‘booboo’, ‘(‘, ‘)’ is recognised as functional macro invocation and it should be expanded to ‘aaa’ and imho in output should look like ‘aaabbb’. I said look like since – to human – it would look like one token whereas compiler would get 2 tokens ‘aaa’ and ‘bbb’ since no ‘##’ operator was used that allows pp-token concatenation. Why/what rule makes cpp (c preprocessor) place additional space between ‘aaa’ and ‘bbb’ when ‘booboo().bbb’ results in ‘aaa.bbb’ without space?

Is this because cpp tries to make output (which is for humans mostly) unambinuous? Human is not able to tell that ‘aaabbb’ is composed from 2 tokens as it sees token’s spelling only. Am I right? I’ve read C99 documentation about preprocessor and gcc’s documentation for cpp. I see nothing about it.

If I am right we have similar situation here:

#define baba() +
baba()+
baba()-

results in:

+ +
+-

Otherwise (if ‘++’ is the output) it would look to a human like ‘++’ token but there would be 2 tokens ‘+’ and ‘+’. Is it like with ‘##’ operator that cpp checks if concatenation produces valid token but in shown cases wants to prevent human that concatenation was performed? ‘+-‘ is not ambiguous hence no space added


Source: c#

Leave a Reply