How these macros are evaluated?
# define i 20
void fun();
int main(){
printf(\"%d\",i);
fun();
printf(\"%d\",i);
return 0;
}
void fun(){
#undef
Preprocessor symbols most definitely have a scope, but that scope does not interact with the other scopes such as file scope.
Preprocessor symbol scope is confined to a single translation unit. A #define
in one translation unit has no bearing on another translation unit.
The scope of a preprocessor symbol is the region of tokens following the directive which #defines
that symbol it. Thereafter, occurrences of the macro are recognized and expanded according to the governing rules. Preprocessor macro definitions are not recursive. If the replacement token sequence contains what look like invocations of the symbol that is being defined, those are not recognized as such. This is why the scope begins after the directive. However, this is still true when a macro is redefined; a redefinition is special and must conform to the rule that it is the same as the original definition. (The precise rules for sameness are in the standard).
The scope of a preprocessor symbol ends with the end of the translation unit, or earlier if it is subject to an #undef
directive.
Thus the scope of a preprocessor symbol is essentially the region of the translation unit text, regarded as stream of preprocessor tokens, in which that symbol is eligible for recognition and substitution.