I'm experimenting to see how far I can abuse the C preprocessor and I have stumbled across an interesting problem.
I have the following macro defines:
#define if(x) if (x)
#define do {
#define elif(x) } else if (x) {
#define else } else {
#define done }
Which should allow me to write:
if (i == 1)
do
...
elif (i == 2)
...
else
...
done
And it works perfectly fine if I only use if
and else
, except the introduction of elif
is problematic because the macro expands as:
} } else { if (x) {
due to the else
being defined.
Is there any way I can get elif
to use 'raw' else
without having it picked up by the preprocessor? I think I need to try nesting multiple defines to trick the preprocessor into pasting the word directly without parsing it but I'm not sure how to achieve this.
Any ideas, or is this not possible in GCC?
Edit:
In essence, this can be boiled down to the following problem:
#define A B
#define B C
For the two given defines A
and B
, how can I get A
to still resolve to the literal word B
and not go through the second define and end up as C
?