How does the following piece of code work, in other words what is the algorithm of the C preprocessor? Does this work on all compilers?
#include
The preprocessor just replaces the symbols sequentially whenever they appear. The order of the definitions does not matter in this case, b
is replaced by a
first, and the printf statement becomes
printf("%i", a);
and after a
is replaced by 170
, it becomes
printf("%i", 170);
If the order of definition was changed, i.e
#define a 170
#define b a
Then preprocessor replaces a
first, and the 2nd definition becomes
#define b 170
So, finally the printf statement becomes
printf("%i",170);
This works for any compiler.