edited by
324 views
1 votes
1 votes
int a=1,b=2;

main()
{
    int a=20,b=30;
    pf(a,b);
    C();
    pf(a,b);
    D();
}

C()
{
    int a=50;
    pf(a,b);
    D();
    pf(a,b);
}

D()
{
    pf(a,b);
    a=3,b=4;
    pf(a,b);
    E();
}

E()
{
    int b=6;
    pf(a,b);
    a=7,b=8;
}


what will be the outputs if static and dynamic scoping is used?

edited by

1 Answer

Best answer
2 votes
2 votes
Using static scope ,lets execute propgram line by line

As we can see in first line variable are declare as global so they are will reside in static area of memory (a=1,b=2).

In main(){ variable a and b are locally declared so 1st pf(20,30).now in next line C() is called .in c() ,a=50  locally declared so in pf  value of a=50 and for b global value will be used ,pf(50,2) .now in C() ,D() is called ,so in D() 1st statement as pf(a,b) but a and b are declared so a and b will be used of global ,so pf(1,2) and a and b are updated in next line which will be updated in global variable so now global variable values will become (a=3,b=4).in next pf(3,4),now E() is called in D().in E() b is declared as locally so in next statement pf (a=3 which is globally value,b=6),now a and b updated but a is not declared as local so global value will be updated (a=7).now E()is completed,so D() is completed .global variable values(a=7,b=4) till now, back come to in C() where D() completed,in next line pf (a=50 which is locally declared,b=4 which is global values),now C() is completed.return back to main ()fn in which upto  C() fn lines are already executed,in next line pf(a=20,b=30) .in next line D()is called ,so in D() first line pf (a=7,b=4, both are global value ),in next line a and b are updating values so update will done in global value(a=3,b=4),in next line pf(3,4),in next line, E() is called.in E() first b is declared as local so in next line pf (a=3 which is global ,b=6 local) In next line a and b are updated a is updated as globally and b as locally.global values now(a=7,b=4),now E()is completed,then D() also completed then main also completed.values are printed as i put in comment.

So this very lengthy explanation ,so  keep program side then try to read my explanation.
selected by

Related questions

0 votes
0 votes
2 answers
2
Debargha Mitra Roy asked 5 days ago
82 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
0 answers
3
RahulVerma3 asked Mar 22
138 views
My question is that can we use command line arguments without use of main function's parameters argc and *argv?
0 votes
0 votes
0 answers
4
RahulVerma3 asked Mar 16
138 views
I have a question that can we use command line arguments without main function arguments?int main(int argc, char argv){}