Reputation: 661
int x;
int main() {
x = 14;
f();
g();
}
void f() {
int x = 13;
h();
}
void g() {
int x = 12;
h();
}
void h() {
printf("%d\n",x);
}
If static scoping is used, what is the result? If dynamic scoping is used, what is the result?
Now if I understand scoping right, the difference between static and dynamic scoping is that static makes variables local to a class. So the value x
would be local to void f()
, void g()
and int main ()
and dynamic would make them globally available. I'm just not sure how to apply it to this code. If static scoping was used would it only print the last value (12 from void g()
) and dynamic scoping would be using all the values of x
?
I'm a little confused on how scoping actually works. I know C
uses static scoping though.
Upvotes: 46
Views: 58836
Reputation: 3725
Static scope : Static scope refers to the scope of variable that is defined at compile time. It is always refers to the variable with top level environment.
In this program, the result of static scope is 14 and 14 because f() and g() always return the value of global variable x. f() and g() not dependent on who are calling them.
Dynamic Scope : Dynamic scope refers to scope of a variable that is defined at run time. It refers to the identifier with the most recent environment. It is something like dynamic programming because in dp the value is updated.
In this program, the result of dynamic scope are 13 and 12 because f() and g() return current variable x value not the global one.
Reference : GeeksforGeeks
Upvotes: 0
Reputation: 9
This is the exactly proven ans in
static scoping: Once the global variable is assigned a value it's scope will exist throughout the program execution.
Hence the answer for this is : 14
and 14
.as C-language posses static scoping u can check it in TURBO-C compiler.
Where as when u come to
Dynamic scoping : Irrespective of the value of global variable .The value assigned in the block is considered.
therefore the Answer is : f()=13
and g()=12
Upvotes: 0
Reputation: 880
Consider the following code snippet
int x=10;
Here the value 10 is stored in a particular location in memory and the name 'x' is bound to that location.
Scope is the part of the program where this Binding is valid. In simple words, the part of program where the variable is visible.
The "part of the program" may refer to a particular portion of code (Static scope) or portion of runtime (Dynamic scope), where this binding is valid.
In this case
int x; //X1
int main() {
x = 14;
f();
g();
}
void f() {
int x = 13; //X2
h();
}
void g() {
int x = 12; //X3
h();
}
void h() {
printf("%d\n",x);
}
X1 can be accessed anywhere in the program. So in main() X1 is assigned the value 14.
When f() is called, a new variable(stored in a new memory location) 'x'(X2) local to f() is created and initialized the value 13 and when g() is invoked,similarly another new variable 'x'(X3) local to g() with value 12 is created.
In Static scoping
when f()or g() calls h(), X2 and X3 are not visible outside f() and g() respectively(portion of code),while X1 is globally visible so the output will be
14
14
In Dynamic scoping When f() executes the name 'x' is binded to X2 and when f() calls h(),the function f() is still executing and the name 'x' is still binded to X2 so the local X2 will be printed to the output. After f() is executed,this binding does not exist(portion of runtime) and 'x' is binded to X1. And when g() executes , the name 'x' is binded to X3.And when h() is invoked, f() is still running and the name 'x' is still binded to X3 so the local X3 will be printed and the output will be
13
12
Upvotes: 7
Reputation: 1506
C/C++ doesn't use Dynamic scoping. Your programming language will use one or the other, you don't get to choose (Unless you are using Clojure! according to Idan Arye below).
Here is a great explanation/comparison with a good example: http://msujaws.wordpress.com/2011/05/03/static-vs-dynamic-scoping/
Upvotes: 15
Reputation: 12633
Static scoping means that x
refers to the x
declared innermost scope of declaration that has one. Since h
is declared inside the global scope, the innermost x
is the one in the global scope(it has no access to the x
s in f
and g
, since it was not declared inside them), so the program prints 14
twice.
Dynamic scoping means that x
refers to the x
declared in the most recent frame of the call-stack the has one. If C used dynamic scoping, h
would use the x
from either f
or g
- whichever one that called it - so the program would print 13
and 12
.
Upvotes: 55
Reputation: 49920
In static scoping, the scope of an indentifier is determined by its location in the code, and since that doesn't change, the scope doesn't either. In dynamic scoping, the scope is determined by the sequence of calls that has led to the use of an identifier, and since that can be different each time that use is reached, is dynamic.
In your example, under static scoping, the x in h will always be the global x, regardless of how h came to be called. But with dynamic scoping, it would refer to the x declared in f or the one declared in g, depending on which had called h that particular time.
Upvotes: 3