Macro replacement list rescanning for replacement

Consider the following simple example:

#include<stdio.h>

const int FOO = 42;

#define FOO (42 + FOO) 

int main()
{
   printf("%d", FOO);
}

Here the output will be 84.

The printf will be expanded to:

printf("%d", 42 + 42);

This means that when the macro FOO is expanded, the expansion will stop when the second FOO is found. It will not be further expanded. Otherwise, you will end up with endless recursion resulting in: 42 + (42 + (42 + (42 + ....)

Live demo here.


The (original) macro being replaced is not FOOBAR, it's INVOKE. When you're expanding INVOKE and you find FOOBAR, you expand FOOBAR normally. However, if INVOKE had been found when expanding INVOKE, it would no longer be expanded.

Let's take the following code:

#define FOOBAR(a, b) printf(#a #b)

#define INVOKE(a, b) e1 a##b(a, b)

int main() {
    INVOKE(INV, OKE);
}

I added the e1 to the expansion of INVOKE to be able to visualise how many expansions happen. The result of preprocessing main is:

e1 INVOKE(INV, OKE);

This proves that INVOKE was expanded once and then, upon rescanning, not expanded again.

[Live example]