edited by
3,946 views
13 votes
13 votes
int a=0,b=0;

int main(void)

{
    int a=3;
    printf("%d%d",a,b);
    c();
    printf("%d%d",a,b);
    return 0;
}
void c()
{
    printf("%d%d",a,b);
    a=4,b=5;
    printf("%d%d",a,b);
    d();
    printf("%d%d",a,b);
    
}
void d()
{
    int b=6;
    e(a,b);
    printf("%d%d",a,b);
    
}
void e(int b,int a)
{
    printf(a,b);
    a=7;b=8;
    printf("%d%d",a,b);
}

 

edited by

1 Answer

Best answer
16 votes
16 votes

A. Static scoping (if variable not found go for global variable)
3 0 (main's local a and global b)
0 0 (global a and global b)
4 5 (global a and global b)
6 4 (e()'s local a,b)
7 8 (e()'s local a,b)
4 6 (global a and d()'s local b)
4 5 (global a and global b)
3 5 (main's local a and global b)

B. Dynamic scoping (if variable not found look in caller function for variable)
3 0 (main's local a and global b)
3 0 (main's local a and global b)
4 5 (main's local a and global b)(main's local a is updated, global a is still 0 and global b=5)
6 4 (e()'s local a and b)
7 8 (e()'s local a=7 and local b=8)
4 6 (main's local a and d()'s local b)
4 5 (main's local a and global b)
4 5 (main's local a and global b)

edited by

Related questions

0 votes
0 votes
2 answers
1
1 votes
1 votes
2 answers
2
3 votes
3 votes
2 answers
3
set2018 asked Oct 10, 2017
446 views
int i=10;main(){int i =5,k,i=6;printf("enter value");scanf("%d",&k);if(k>0){fun1();}else{fun2();}}void fun1();{printf("%d",i);}void fun2();{int i=5;printf("%d",i);fun1();...
3 votes
3 votes
1 answer
4
dragonball asked Jan 14, 2018
880 views
How to solve any dynamic scoping question . Plz describe in detail.