• edited by
6,382 views
15 15 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);
}

 

1 Answer

Best answer
17 17 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
Position:
Show:

Related questions

0 0 votes
2 2 answers
872
872 views
Hradesh patel asked Jun 20, 2018
872 views
Q. int k; fun(int i) { k = i + 3; printf(k); } gun( int i) { int k = 4; k = i+4; fun(k); printf(k); } main(){ k=2 ; gun(k); printf(k); }my resultstatic scoping - 9,6...
1 1 vote
2 2 answers
5.1k
5.1k views
Ayush Upadhyaya asked Jan 3, 2019
5,073 views
#include<stdio.h int x=10; int main() { static int x=10; x+=f1()+f2()+f3()+f1(); printf("%d",x); return 0; } int f1(){static int x=25;x++;return x;} int f2(){int x=50;x++...
3 3 votes
2 2 answers
828
828 views
set2018 asked Oct 10, 2017
828 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 3 votes
1 1 answer
1.3k
1.3k views
dragonball asked Jan 14, 2018
1,253 views
How to solve any dynamic scoping question . Plz describe in detail.