edited by
825 views
1 votes
1 votes

What will be the output of following program if we use a) static scoping b) dynamic scoping?

int a= 1,b=2;

main( ) {

int a=20,b=30;

Print(a,b); C( );

Print(a,b); D( ); }

C( ) {

int a= 50;

print (a,b);

D( );

Print(a,b);

}

D( ) {

Print (a,b);

a=3; b=4;

Print(a,b);

F( );

}

F( ) {

int b =6;

Print( a,b);

a=7; b=8;

}

edited by

1 Answer

Best answer
2 votes
2 votes

When in a function, a variable which hasn't been declared inside the function is used, the scope of the variable is ambiguous. This problem is resolved in two ways :

1.Static Scoping (when Compiler resolves the problem) It uses the memory of those variables (with same name) that have been allocated memory at compile time in the static area i.e global variables

2. Dynamic Scoping (when Processor resolves the problem) It uses the memory of those variables (with same name) that have been most recently used in the runtime stack. i.e local variables of the other functions.

selected by

Related questions

0 votes
0 votes
1 answer
2
Rohit Chakraborty asked Oct 6, 2023
491 views
What is the correct option for the blanks so that the program runs in the correct manner?I was getting the answer B
3 votes
3 votes
1 answer
3
TusharKumar asked Jan 31, 2023
537 views
answer given is 33