-2

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.

The Challenge is in Static Scope we use a=1, b=2, c=3 without attention to main or no, use a=1, b=2, c=4? Just in Static Scope without including C Rules.

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);
}
  • 2
    You may have misinterpreted what your teacher says. In C, this code can only give one output: `124`. – kaylum Feb 11 '16 at 23:32
  • The interesting thing is that the code doesn't output `1 2 3` or `2 3 4`. So maybe the first step to understanding the code is to actually run the code and see what it outputs. – user3386109 Feb 11 '16 at 23:33
  • 1
    You need to explain (demonstrate) how the scope is changed between 'static' and 'dynamic'. As written, the code can only produce one answer — and that's `124`. Note that printing operations should generally end with a newline, too — that's sloppy style on display. You could get `123` if the reference to `c` in `main()` was prefixed with `int`; you could get `234` if the references to `a` and `b` in `fun1()` were not prefixed with `int`. – Jonathan Leffler Feb 11 '16 at 23:35
  • @kaylum dynamic and static scope is differ. –  Feb 11 '16 at 23:37
  • 2
    Sorry, that statement does not make sense in the context of the code shown and the question you asked. What do you understand to be dynamic and static scope in C? And how can that possibly affect the output of the code *as shown*? Or are you asking how can the code be changed to produce the different suggested outputs? – kaylum Feb 11 '16 at 23:38
  • 1
    @MaryamPanahi: How are the dynamic and static scopes different? What changes in the code between the two? It isn't standard C terminology, which is one reason why there is confusion. The words 'dynamic', 'static' and 'scope' are used individually, but not usually as 'dynamic scope' or as 'static scope'. – Jonathan Leffler Feb 11 '16 at 23:40
  • 2
    You need to answer, or ask the teacher, what on earth is meant by 'dynamic scope' here. It is meaningless to me in the context of this code, and I'm both a compiler writer and a 30+-year user of C. Either you or he is misusing standard terminology. All the scopes shown here are static. The first declaration of `a,b,c` has static *linkage*, and the other declarations have automatic *allocation.* – user207421 Feb 11 '16 at 23:49
  • @EJP maybe means of my teacher is simulating dynamic scope on pesuedocode like C ? –  Feb 11 '16 at 23:57
  • @MaryamPanahi Maybe anything. With the information you've provided it is impossible to say, and speculating about it is pointless. You've tagged the question as C, and in C all the above comments apply, except yours: there is no such thing as dynamic scope, and this code does not produce either of the outputs claimed. – user207421 Feb 11 '16 at 23:59
  • 1
    Duplicate of [Lexical scoping vs dynamic in C](http://stackoverflow.com/q/19574605/207421). – user207421 Feb 12 '16 at 00:05
  • @EJP my last question is in static scope the result is 1 2 4 surely without any assumption. am I right? –  Feb 12 '16 at 00:30
  • @EJP As "compiler writer" you should've heard of it: https://en.wikipedia.org/wiki/Scope_(computer_science)#Dynamic_scoping – Daniel Jour Feb 12 '16 at 00:38
  • @DanielJour Read what I wrote. I chose my words with care. I didn't say I'd never heard of it. I said it is 'meaningless to me in the context of this code'. – user207421 Feb 12 '16 at 00:56
  • @EJP Ah ... expanding the comments shows the link you posted. I just wanted to share the information with you in case you (as I presumed) didn't know about it. (One can have a lot of fun with dynamic scope .. retro fitting a condition system, for example) I hope I didn't sounded offensive (if so I'm sorry, my comment wasn't meant to be offensive) – Daniel Jour Feb 12 '16 at 01:04
  • The primary problem was that the question was tagged [tag:c] but it makes no sense as C. The problem was then compounded when someone removed the tags about [tag:compiler-construction] and [tag:programming-languages]. Now that it is not tagged [tag:c], it makes more sense, though using C functions such as `printf()` still has the potential to cause some confusion. In this context, using some pseudo-language construct (e.g. `print a, b, c;`) along with an explanation that this is _not_ C code would have stopped people going down the wrong path. The comments above a mainly appropriate for C. – Jonathan Leffler Feb 12 '16 at 02:00
  • @JonathanLeffler there is one problem I think... In static scope the fun2fun2 takes the globally scoped cc which comes from scope where function is defined (opposite to dynamic, where it traverses scopes of execution), so c=3 not c=4 ! –  Feb 12 '16 at 08:49
  • Cross-posted: https://cs.stackexchange.com/q/52990/755 – D.W. Jan 17 '21 at 07:56

1 Answers1

0

If dynamic scope were possible in C, then lookup of the variables a, b and c inside of fun2 would use the dynamic environment.

This, in turn, depends on how the function got actually called. Since it's called from fun1 the variable bindings of that scope will be used (thus a = 2 and b = 3). Because fun1 had been called from main, which set up the binding c = 4, the output would be 2 3 4.


Another example:

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

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

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

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

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

void fun3() {
  int c = 42;
  fun2();
}

Would print

2 3 4
21 2 42

Perhaps actually seeing the difference helps you. Clojure supports both dynamic and lexical scoping (this is the correct terminology, btw):

(def static-scoped 21)
(def ^:dynamic dynamic-scoped 21)

(defn some-function []
  (println "static = " static-scoped)
  (println "dynamic = " dynamic-scoped))

(defn other-function []
  (binding [dynamic-scoped 42]
    (println "Established new binding in dynamic environment")
    (some-function)))

;; Trying to establish a new binding for the static-scoped
;; variable won t affect the function defined
;; above.
(let [static-scoped 42]
  (println "This binding won't affect the variable resolution")
  (other-function))

(println "calling some-function directly")
(some-function)

(live)

Note that Clojure tries to be as purely functional as possible, thus none of the code above is a assignment (in other words: the values of the variables aren't modified once assigned)

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
  • 1
    @user3386109 The question is tagged with compiler construction and programming languages. I'd guess this is from a compiler / programming languages class, possibly using a subset of C as language for some experiments. – Daniel Jour Feb 11 '16 at 23:39
  • @DanielJour you are correct from the Programm Lang.. Class. –  Feb 11 '16 at 23:47
  • Indeed. It's a "what if C had.." type of question. – Daniel Jour Feb 11 '16 at 23:47
  • would you please more inspect on Static and Dynamic Differ in your answer? –  Feb 11 '16 at 23:49
  • One depends on the (static) location in the source code, the other on the (dynamic) history that got you were you are during runtime. You'd need to be a bit more precise about what exactly you don't understand. – Daniel Jour Feb 11 '16 at 23:51
  • How we reach to 1 2 3 ? –  Feb 11 '16 at 23:53
  • 1 2 3 will be the result (if you change the line in `main` from `c = 4` to `int c = 4`) under static scope because variable lookup then only uses syntactically higher levels. That's: if no binding for `a` is found in the current scope it'll look in the containing scope, which is the global scope with `a = 1` in your example) – Daniel Jour Feb 11 '16 at 23:56
  • You means in static just use the higher level definition ? –  Feb 12 '16 at 00:03
  • There is something Wrong ! I get 1 2 4 not 1 2 3. –  Feb 12 '16 at 00:25
  • Please *read all* of my comment: "[..] (if you change the line in main from `c = 4` to `int c = 4`) [..]" – Daniel Jour Feb 12 '16 at 00:39
  • I Get it. I think in Static Rule we dont enter in main() and use a=1,b=2,c=3, but if we use C Rules , 1 2 4. –  Feb 12 '16 at 00:42
  • No. Without the above change it's never `1 2 3` because you *change* the global `a` to `4` inside of `main`. – Daniel Jour Feb 12 '16 at 00:43
  • Daneil as my last questions is it possible to implement it in pascal on online IDE to see the result? –  Feb 12 '16 at 01:03
  • I don't know (haven't done much with Pascal, and it's been a while) but probably not, because this is part of the fundamental machinery of a programming language and as such cannot be changed (without ugly hacks). – Daniel Jour Feb 12 '16 at 01:10
  • You could implement a simple Lisp and play with the different scoping models, though. – Daniel Jour Feb 12 '16 at 01:11
  • I want to test it in a another static scope language, just test. would you please help me now ? –  Feb 12 '16 at 01:14
  • In static scope the fun2fun2 takes the globally scoped cc which comes from scope where function is defined (opposite to dynamic, where it traverses scopes of execution), so c=3 !! –  Feb 12 '16 at 08:48
  • You seem to have a serious confusion between scope and actual execution path. With lexical scope it's `c = 4` because in main the `c` is resolved to the global one and then assigned that new value, overwriting the previously stored 3. – Daniel Jour Feb 12 '16 at 10:08
  • Is it possible to make it clear for me Daniel? –  Feb 12 '16 at 10:11