程式語言的scopes的問題

Q: Consider the following skeletal C program

void fun1(void); /*prototype*/

void fun2(void); /*prototype*/

void fun3(void); /*prototype*/

void main(){

int a,b,c;

...

}

void fun1(void); {

int b,c,d;

...

}

void fun2(void); {

int c,d,e;

...

}

void fun3(void); {

int d,e,f;

...

}

Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last function called?Include with each visible variable the name of the function in which it was defined.

a. main calls fun1; fun1 calls fun2; fun2 calls fun3.

b. main calls fun1; fun1 calls fun3.

c. main calls fun2; fun2 calls fun3; fun3 calls fun1.

d. main calls fun3; fun3 calls fun1.

e. main calls fun1; fun1 calls fun3; fun3 calls fun2.

f. main calls fun3; fun3 calls fun2; fun2 calls fun1.

1 Answer

Rating
  • 其威
    Lv 7
    10 years ago
    Favorite Answer

    in fact you can't use the function name as the scope name to reference variable in different function, because they're actually function name not scope name or class names.

    I'm using funX::var here just to distinguish variables in different functions.

    for example there're both variable c in main(), fun1(), and fun2(), to distinguish between I write main::c, fun1::c, and fun2::c

    a. fun3::d, fun3::e, fun3::f

    b. fun3::d, fun3::e, fun3::f

    c. fun1::b, fun1::c, fun1::d

    d. fun1::b, fun1::c, fun1::d

    e. fun2::c, fun2::d, fun2::e

    f. fun1::b, fun1::c, fun1::d

Still have questions? Get your answers by asking now.