• edited by
877 views
0 0 votes

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 result

static scoping - 9,6,2   ( here last print statement i have doubt ..it will print 2 or 9)

dynamic scoping- 9,9,2

2 Answers

1 1 vote
In Static Scoping, if a variable is not defined in the Local space, it is looked in Globle Space.

In Dynamic Scoping,  if a variable is not found in the Local space , it is looked in the place, where it recursive called.

In Static

gun() have local variable 'k' and pass updated value "6"  ............

In fun(), there are no loval variable 'k' so in static dynamic it use GLOBLE variable and update then with  k = i + 3   = 6+3 = 9

So in main() , print() print 9  .

Hope you will understand.
Position:
Show:

Related questions

15 15 votes
1 answers 1 answer
6.4k
6.4k views
firki lama asked Jan 9, 2017
6,396 views
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("%...
10 10 votes
4 4 answers
341
341 views
GO Classes asked Jun 10
341 views
Given the following two files, what will happen when they are compiled and linked together?file1.cstatic int count = 3; void update() { count = count + 1; }main.c#include...
8 8 votes
2 2 answers
277
277 views
GO Classes asked Jun 9
277 views
What is the output of the following code?#include <stdio.h void fun() { static int x = 1; x = x + 2; printf("%d ", x); } int main() { fun(); fun(); fun(); return 0; }$\te...
2 2 votes
5 5 answers
1.8k
1.8k views
admin asked Mar 31, 2020
1,827 views
How will you free the allocated memory?remove(var-name)free(var-name)delete(var-name)dalloc(var-name)