No, that will not work. The C / C ++ preprocessor is just a “text time processor”. Thus, it works with text found as it is in your source code.
That's why it takes the literal text "i", passes it to your macro, expanding it into the literal text "xi" in the source code. This is then passed to the compiler. The compiler then parses the text processed by the post-processing, and detects the letter token "xi" as an undeclared variable, moving up to the process.
You can take the source code of the sample and pass it to the gcc compiler (for example, I used gcc under cygwin, pasting your code into a file that I named pimp.c, due to the lack of a better name). Then you get the following:
$ gcc pimp.c pimp.c: In function `main': pimp.c:9: error: `xi' undeclared (first use in this function) pimp.c:9: error: (Each undeclared identifier is reported only once pimp.c:9: error: for each function it appears in.)
In short, no, you cannot do this. To be able to do just that, the preprocessor had to act as an interpreter. C and C ++ are (usually) non-interpretable languages, and the preprocessor is not an interpreter. My suggestion was to clearly understand the differences between compilers and translators (and between compiled and interpreted languages).
Sincerely.
luis.espinal
source share