I just followed a tutorial that involved doing CPP macros to implement a Debug system in a program. One great behavior of the macros is being recursive, making it possible to put a macro inside another macro like below:
#define MACRO1 "World"
#define MACRO2 printf("Hello %s\n",MACRO1);
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]){
MACRO2
return 0;
}
Output: Hello World
The below also seems to work:
#define MACRO2 printf("Hello %s\n",MACRO1);
#define MACRO1 "World"
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]){
MACRO2
return 0;
}
So just to understand, does CPP first reads all #define X
to make a list of declared macros, to then substitute the macros that are inside other macros, avoiding the "chicken and egg" issue on pre-processing?
I think that makes sense, considering pre-processing is a one-time only process (during compilation), not happening in real time. So it shouldn't matter where in the code a macro was defined, but actually if it was defined at all.
Having a 3000 lines code and only in the last line defining a macro used in the code would be valid then?
Thank you in advance!