# What are differences between Static Scope and Dynamic Scope?

My teacher has provided the following pseudo-code, and says that the output using static scope is 1 2 3, but the output using dynamic scope is 2 3 4.

void fun1(void);
void fun2(void);

int a = 1, b = 2, c = 3;

int main() {
c = 4;
fun1();
return 0;
}

void fun1() {
int a = 2, b = 3;
fun2();
}

void fun2(){
printf("%d %d %d", a, b, c);
}


Which variable we use in Static Scope? $c=3$ or $c=4$? (by Static Scope Rule and without considering C Rules).

In static scope the $fun2$ takes the globally scoped $c$ which comes from scope where function is defined (opposite to dynamic, where it traverses scopes of execution), so $c = 3$.
With dynamic scope traverses up the chain of scopes until it finds variable it needs, which reflects the fact that scopes are:
Global($a=1, b=2, c=3$) -> main($c=4$) -> fun1 ($a=2, b=3$) -> fun2.
fun2 in the way up finds $a=2, b=3$ in fun1 and $c=4$ in main.
By the way: C has static scope rules.

• Yes, we have $1 2 3$ by static rules. I added one missing int to variable C, after edit. In the original question there was 4, which was not by static/dynamic rule but came from simple assingment. Sorry if I changed question too much, but it was off topic as only checking code.
– Evil
Feb 12 '16 at 12:31
• Yes, it is 1 2 4 in static / C
– Evil
Feb 12 '16 at 12:48
• You use global c. And classic assungment overwrites global one - if this was not variable then it was working assingment - after execution the global c=3 gets c=4.
– Evil
Feb 12 '16 at 12:52

What is static scope ? Static scope refers to scope of a variable is defined at compile time itself that is when the code is compiled a variable to bounded to some block scope if it is local, can be bounded to entire Main block if it is global. examples: C,C++, Java uses static scoping

What is dynamic scope in Programming Languages ? Dynamic scope refers to scope of a variable is defined at run time rather than at compile time. Perl language allows dynamic scoping.

Coming to your question a=2,b=3 are defined in fun1() which is the most recent scope and dynamic scope for a,b variables . So, if we use dynamic scoping output will be 2,3,4. For clear information check this link static scope and dynamic scope with examples.

• The text here appears to be copied word-for-word from youtube.com/watch?v=6m_RLTfS72c (i.e., your link), without indication that it has been copied. Copying without indication that the material has been copied, or without proper attribution, is frowned upon by our community. Please consult our guidelines on crediting your sources and edit your answer so it complies with those guidelines. Thank you!
– D.W.
Feb 22 '17 at 18:21
• I've edited it for you. Please do look at our guidelines. Thank you!
– D.W.
Feb 23 '17 at 15:32

POINT OF CLARIFICATION:

The 'c' referred to in main() IS NOT DECLARED IN main(). Therefore there is only one 'c' in the program, the global one. When the program is run, this 'c' is first initialized to 3, then the value 3 is overwritten in main() with 4, which is why the output of fun2() is '1 2 4'.

If we alter the statement in main() from 'c = 4' to 'int c = 4', THEN there will be two 'c' variables, one global, the other local to main(). THEN we can say that the output of fun2() should be '1 2 3' under static scoping (as in C) versus '1 2 4' under dynamic scoping.

Each procedure that is invoked contains a link (the dynamic link) to the invoking procedure. When the procedure is finished, this link is used to return to the invoking procedure. In a language using dynamic scoping,the dynamic link is also used to lookup the 'scope' of the variables. That means that in the example fun2 is invoked by fun1. By going back the dynamic link, we find that a=2 and b=3. To find c we follow the dynamic link to main, and c=4.

In a static scoped (also called lexical scoped) language the dynamic link is only used to find the return address when the procedure is finished. A second link (called the static link) is defined to find the scope of the variables. In the example all three functions (fun1, fun2 and main) are defined on the same level. All three of them will have a static link pointing to the global level. So when fun2 is invoked, it will follow the static link to the global level and find a=1, b=2 and c=3.