I know that probably there is no concept of scope for macros, but please help me understand the following output – which seems to suggest that macros are local to functions:
#include<stdio.h>
#include<stdlib.h>
#define A 100
void fun();
int main()
{
fun();
printf("%d\n",A);
system("pause");
return 0;
}
void fun()
{
#undef A
}
The output of the program is 100 though according to me it should have been a compiler error. Please explain why?
The pre-processor works on the text of your source code and it does it before the compiler proper ever starts to run.
In essence your compiler works on a file that looks like
So running and printing 100 is exactly what you would expect.
Notice that all the pre-processor directives are gone, and that all instances of
Abetween thedefineand theundefhave been replaced with100.The thing to remember is: