Computer Science, asked by ManojGhera3833, 1 year ago

Difference between static and dynamic scope of variable

Answers

Answered by arpi7494
0


int x; int main() { x = 14; f(); g(); } void f() { int x = 13; h(); } void g() { int x = 12; 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?

Now if I understand scoping right, the difference between static and dynamic scoping is that static makes variables local to a class. So the value xwould be local to void f(), void g() and int main () and dynamic would make them globally available. I'm just not sure how to apply it to this code. If static scoping was used would it only print the last value (12 from void g()) and dynamic scoping would be using all the values of x?

I'm a little confused on how scoping actually works.

Answered by Sweetbuddy
2
HEY BUDDY HERE IS UR ANSWER !!!

》Static scope : Static scope refers to the scope of variable that is defined at compile time. It is always refers to the Variable with top level environment. In this program, the result of static scope is 14 and 14 because f() and g() always return the value of global variable x

Hope u like answer !!!!

☺☺ BE BRAINLY ☺☺
Similar questions