• edited by
1,752 views
1 1 vote

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;

}

1 Answer

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

Related questions

0 0 votes
1 1 answer
490
490 views
gaurav_sah asked Apr 23, 2025
490 views
faster access to non local variables is achived using an array pointers to activation records called a stack , heap , display , activation tree?
3 3 votes
1 answers 1 answer
1.1k
1.1k views
saket jaiswal asked Jan 22, 2025
1,122 views
#include <stdio.h int main() { int x = 10, y = 5, z = 2; int result = x y ? y z ? 1 : z x ? 2 : 3 : y z ? 4 : z x ? 5 : 6; printf("%d", result); retu...
0 0 votes
1 1 answer
909
909 views
Rohit Chakraborty asked Oct 6, 2023
909 views
What is the correct option for the blanks so that the program runs in the correct manner?I was getting the answer B