Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Scope (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Macro expansion === {{main article|Macro expansion}} In modern languages, [[macro expansion]] in a [[preprocessor]] is a key example of de facto dynamic scope. The macro language itself only transforms the source code, without resolving names, but since the expansion is done in place, when the names in the expanded text are then resolved (notably free variables), they are resolved based on where they are expanded (loosely "called"), as if dynamic scope were occurring. The [[C preprocessor]], used for [[macro expansion]], has de facto dynamic scope, as it does not do name resolution by itself and it is independent of where the macro is defined. For example, the macro: <syntaxhighlight lang=c> #define ADD_A(x) x + a </syntaxhighlight> will expand to add <code>a</code> to the passed variable, with this name only later resolved by the compiler based on where the macro <code>ADD_A</code> is "called" (properly, expanded). Properly, the C preprocessor only does [[lexical analysis]], expanding the macro during the tokenization stage, but not parsing into a syntax tree or doing name resolution. For example, in the following code, the name <code>a</code> in the macro is resolved (after expansion) to the local variable at the expansion site: <syntaxhighlight lang=c> #define ADD_A(x) x + a void add_one(int *x) { const int a = 1; *x = ADD_A(*x); } void add_two(int *x) { const int a = 2; *x = ADD_A(*x); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)