Jump to content

Dynamic and Static Scopes

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
atrip25

atrip25

    Newbie

  • Members
  • PipPip
  • 26 posts
Hi all,

I'm new to the forum and pretty new to programming. I'm taking a Comparitive programming course and working on figuring some of this stuff out. I'm working on a problem that deals with dynamic and static scoping. I'm not looking for someone to answer the question for me or give me the answer. I'm wanting some explaination and guidance on how this stuff actually works. For example:

Consider the following C program:
     int x;

     int main() {
         x = 20;
         f(); 
         g();
     }

     void f() {
         int x = 30;
         h();
     }

     void g() {
         int x = 40;
         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? Which scope rule is actually used by C language?

If I understand this correct. The results under dynamic scoping would be x = 40 and static would be x = 20. :confused: Also I believe if I found the correct information C programming uses static scoping and not dynamic. The problems I am posting are not homework assignments, they are problems that are given for review and understanding so they do not get submitted for a grade. Let me know if you can help. Thanks.

Edited by Orjan, 20 October 2009 - 08:28 AM.
Use code tags for showing code


#2
HumbleMumbleWump

HumbleMumbleWump

    Newbie

  • Members
  • Pip
  • 6 posts
I am unfamiliar with the terms "dynamic scoping" and "static scoping", one minute...(looks at Wikipedia article)

You are correct, C does use static scoping. Since C uses static scoping h() will print the top-level binding of x available to it, which in this case is the global variable x declared above main() (and assigned the value of 20 before either f() or g() are called). Also note that if the global variable x had not been declared above main() you would get a compiler error stating something along the lines of "line 21 -- undeclared variable 'x'" since h() was not passed a value of x to use nor was a new variable labeled 'x' declared inside h(). Hence the output should look like this:
20
20

However I believe you are not correct about the results under dynamic binding. After reading the Wikipedia article under dynamic scoping the output would look like this:
30
40
(after g() is called highest binding left is x=20, the global scope)

When f() is called it creates a new higher-level binding for x, then assigns the value 30 to that binding. Since the call to h() is still part of the function f() this temporary binding is still in effect, and h() prints "30\n". After h() is called in f() control returns to main() and this temporary binding disappears and we resume using the global binding. The same thing happens when g() is called except x is assigned the value 40.