0

I have read this question, and I think I've understood the most upvoted answer, but he said

since basically every programming language in wide use today uses lexical scoping

I also heard this from coursera programming language, but here's a simple C code:

#include <stdio.h>

int x = 1;

void fun(){
    printf("%d\n", x);
}

void dummy1(){
    x = 2;
    fun();
}

void dummy2(){
    x = 3;
    fun();
}


int main(){

    x = 4;

    fun();
    dummy1();
    dummy2();

    return 0;
}

output:

4
2
3

C++ have exactly the same behavior, so I think C and C++ are dynamic scoped language, are they? And is it real that most programming languages use static scope?

alandawkins
  • 309
  • 3
  • 10
  • The full sentence in that answer is *This is likely a very unfamiliar concept, since basically every programming language in wide use today (except perhaps emacs lisp) uses lexical scoping* which would make you think that includes C and C++(those would be pretty hard to forget if they were different) – NathanOliver Mar 15 '18 at 13:04
  • 3
    All of those functions are using the same global variable. This isn't showing dynamic scoping. – Kevin Mar 15 '18 at 13:04
  • 5
    Dynamic scoping is when you introduce a new variable (i.e. `int x = 3`) and functions you call can see it. Here you aren't introducing any new variables but using the same global one. – Kevin Mar 15 '18 at 13:06
  • 1
    You only have one x definition here, so *static lexical scoping* and *dynamic scoping* would behave the same anyway. And both C and C++ use *static lexical scoping*. – Jarod42 Mar 15 '18 at 13:10
  • I really think you should widen your language vocabulary and investigate LISP, SNOBOL, BASIC and ADA. Not all languages have scoping rules. – Thomas Matthews Mar 15 '18 at 15:57
  • @ThomasMatthews Thanks for your advice! I've touched C/C++ and a little bit python, and am learning SML to understand some common basics of programming language! – alandawkins Mar 16 '18 at 06:52

1 Answers1

3

What you have is not dynamic scoping. You aren't introducing any new variables but using the same global one. If C and C++ had dynamic scoping then this (note that each x is a new variable):

#include <stdio.h>

int x = 1;

void fun(){
    printf("%d\n", x);
}

void dummy1(){
    int x = 2;
    fun();
}

void dummy2(){
    int x = 3;
    fun();
}


int main(){

    int x = 4;

    fun();
    dummy1();
    dummy2();

    return 0;
}

would output

4
2
3

but instead it outputs

1
1
1

Since fun() is always using the same global x initialized to 1. This is because C and C++ use static lexical scoping.

Kevin
  • 6,993
  • 1
  • 15
  • 24
  • 1
    Another helpful point to illustrate the difference between dynamic scoping and making everything global, would be to add another call to `fun()` after the calls to the `dummy` functions. With dynamic scoping the output should then be `4,2,3,4` whereas OP's version with only a global variable would produce `4,2,3,3`. – sepp2k Mar 15 '18 at 13:21
  • 2
    If dynamic, output should be `4 2 3` or `4 3 2`? – alandawkins Mar 15 '18 at 13:21
  • 1
    Oops, yeah it should be `4 2 3`. I got the order wrong. Edited. – Kevin Mar 15 '18 at 14:11