Static and Dynamic Scoping (Part-3)

preview_player
Показать описание
C Programming & Data Structures: Static and Dynamic Scoping (Part-3)
Topics discussed:
1) What is Dynamic Scoping?
2) Example of Dynamic Scoping.
3) Difference between Static and Dynamic Scoping.
4) Homework problem on Static and Dynamic Scoping.

Music:
Axol x Alex Skrindo - You [NCS Release]

#CProgrammingByNeso #CProgramming #StaticScoping #DynamicScoping
Рекомендации по теме
Комментарии
Автор

The question seems tricky but very simple, if you are clear that global variable is global variable everywhere unless it is declared locally and changed locally.
First globally:
global a=0, global b=0 taken by the compiler

Second it enters main( ):
global a = output generated by fun1( ), global b = 0

Third it enters fun1( ):
global a = 0, global b = 1, local a = 0, local c = 2 (AS IT IS NOW DEALING WITH LOCAL VARIABLES)

meanwhile in main( ):
global a =2

fourth it enters fun2( ):
global a = 3, global b = 1, local b = 4 (AS ONLY "b" IS DEFINED LOCALLY)

Finally print( ):
Static prints global 'a' and 'b'. (a =3, b=1)
Dynamic prints the values it finds in immediate function from where print( ) is called (a=3, b =4)



Hope this helps others who were confused like me! :)

pujithapatnaik
Автор

Extremely helpful series; you're clearly a great instructor.

TheGianaJinx
Автор

Static scoping: b. 3 1
Variables a and b are declared as global which means their initial values are zero. Then in the main function "a" is assigned to fun1(). Inside fun1() the value assigned to "b" is 1 and the value returned is 2 so at that point a = 2, b = 1. Then the next line calls fun2() in which "a" is assigned a different value a = 3 so at that point, the current values are a = 3, b=1. Finally, the print function is called. Keep in mind that fun1() has 2 local variables "a" and "c" and fun2() has one local variable "b". So those values do not affect the global variables.

danieltornero
Автор

Below 4 points are supposed,

1) Static scoping : focus on global variable
2) Dynamic scoping : focus on calling function's variable (rather than global variable)
3) Global variable = let's mark as (G)
4) Local variable = let's mark as (L)

on question,

(G) a, (G) b is declared and it's automatically initialized as "0".
And stack will be as below,

- 4th stack (print function)
- 3rd stack (f2 function) : (G) a =3, (L) b=4
- 2nd stack (f1 function) : (L) a=0, (G) b=1, (L) c=2
- 1st stack (main function) : (G) a = result of f1 function (which is 2)

Once it starts to be executed from 4th → 1st stack,

- Static scoping : Refers below 2 variables
1) (G) a = 3 from 3rd stack
2) (G) b =1 from 2nd stack

- Dynamic scoping : Refers below 2 variables
1) (G) a = 3 from 3rd stack
2) (L) b = 4 from 3rd stack

MisolifeonthePlanet
Автор

Simply make 2 tables for variable a and b(as only 2 global variables defined)....one for global a, b and another one for local variables a, b, c ( as c is defined locally in fun1() go through each function step by step and update the values in both the tables..then see what the output comes at the end.
Static : 3 1
Dynamic : 3 4

Note : default initialized value of global variables are 0.

Hope it helps 🙏

_iamankitt_
Автор

Static scoping:
b) 3 1
Dynamic scoping:
d) 3 4

justpaulo
Автор

It should be noted that most languages only either support static or dynamic scoping. It is not something the user can decide upon. C only uses static scoping. The code shown here for dynamic scoping could be thought of as pseudocode code since C only uses static scoping as pointed out by Neso.

_shubham_kumar
Автор

Everything is ok in your lecture except the last home work part😰

tamil
Автор

Comments for myself: In dynamic scoping, the search method will be: inside the function itself, then towards the caller function and up to succeeding caller functions. It is important to remember though that in C, static scoping is used by functions by default not dynamic. The correct answers in the homework problems are, b and d, respectively.

compangit
Автор

Sir, It will be very helpful if you solve the homework questions also in upcoming lectures. Else the problem gets left out and we remain in doubt where our concepts went wrong.

anjalis_
Автор

Okey for static scoping: 3, 1
Dynamic scoping: 3, 4
But I have doubt, how we ( know / differentiated ) when our function perform static or dynamic scoping... Plz reply if you know 🙏😊✨🤞....

Vishalkumar-fefu
Автор

please make the videos on the examples which you give us as an homework. So we can get it properly.🤗

Aabara_ka_dabara
Автор

Static Function Explanation

In fun1() int a, c are declared and
In fun2() int b is declared





Now lets get to main int a, b; are declared globally

when fun1() is called, a will have 2 and b will have 1, coz in fun1() I havent declared any variable named b so the value goes to global b; so global b=1, global a=2;





when fun2() is called, now we have a=3, b=4 which is locally declared ;


but they havent declared a so 3 goes to global a and b=4 goes to local b;
so global a=3 from 2 and b=1(no change)



then print() it has no local values so it goes for global






Dynamic Function Explanation

get to fun2()

there they have locally declared a=3, b=4;
Now


go to print()
then it looks for a, b in its caller function which fun2(), so the callee print() prints the local variables from fun2()






It takes time to get it, But eventually u will get it .




To understand it better, in fun1() change the declaration int a, c to int a1, c1;
and in fun2() change the declaration int b to int b1;
Despite these changes u will get the same output for static and Dynamic .

bharathvenkateshappa
Автор

@Neso Academy can you pls send the Correct answer in the comments section

rakeshkumarbehera
Автор

static scoping
a)2 4
dynamic scoping
d)3 4

nsolid
Автор

HomeWork Pro-
Static scoping:
b) 3 1
Dynamic scoping:
d) 3 4

ayushpawar
Автор

For dynamic scoping, the answer is 2, 4 and for the static scoping answer is 3, 1.

globalevents
Автор

Static scoping
A=3, B=1
Dynamic scoping
A=2, B=4

jayachandrans
Автор

Sir please make one separate video on all homework you gave to us.

tech_loverhaked_it
Автор

Sir, please make the videos on the examples which you give us as an homework. So we can get it properly.
Please sir, keep an eye on my opinion above mentioned.🙏

nikhilchapne