in Programming in C
2,054 views
1 vote
1 vote
#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++;return x;}
int f3(){x*=10;return x;}

What is the output of the above code, if dynamic scoping is used.

in Programming in C
2.1k views

4 Comments

@shreyansh jain-I am trying to understand the output of the below code

#include <stdio.h>
int fun(int *a)
{
	(*a)++;
	return (*a);
	
}
int main(void) {
	static int x=10;
	x=x+fun(&x)+fun(&x)+fun(&x);;
	printf("%d",x);
	return 0;
}

 

It outputs 47 how?

https://ideone.com/vKy3eq

 

0
0
edited by

@Ayush Upadhyaya 

replace 

x=x+fun(&x)+fun(&x)+fun(&x);​ //47

with

x+=fun(&x)+fun(&x)+fun(&x); //49

and Execute, this is what you are missing I guess! The "Associativity".

_______________________

x+=fun(&x)+fun(&x)+fun(&x);

// this requires everything on RHS to be evaluated, x = x+[fun(&x)+fun(&x)+fun(&x)]

whereas,

x=x+fun(&x)+fun(&x)+fun(&x);​

// this requires immediate operand on RHS to be evaluated, x = [{{x+fun(&x)}+fun(&x)}+fun(&x)]

0
0

Because in C, operation order is undefined. Thus, the following order would lead to 47:

f(&x) : 11
x     : 11
f(&x) : 12
f(&x) : 13
--------------
total : 47
0
0

2 Answers

2 votes
2 votes

  

    x = x + f1() + f2() + f3() + f1();
    x = 10 + 26 + 51 + 100 + 27
    x = 214
/* x is static local variable of main function and its value is 10.
f1() also access its local static variable value that is 25 after increment 26.
f2() access its local variable value that is 50 after increment 51.
f3() access global variable value that is 10 because C uses static scoping. And In dynamic scoping, f3() will access local static variable of main function
f1() access its local static variable which is available throughout the program */
edited by

12 Comments

There is another f1() at the end after f3()
0
0
updated.
0
0
please correct your calculation .
0
0
Avdhesh..f3() will use main's static x and multiply it with 10 right? So it should return 100.

Also your addition should result to 124.
0
0
First x should also update accrodingly,  answer should be 304
0
0
0
0
But C follows static scoping ..the answer won't match..
0
0

i got same ans but given ans is

 

but  acc to me 214 is right

0
0
They took initial value of x as 1 and in the program they gave x=10 -_-
0
0
I think 304 should be the answer..
1
1

@MiNiPanda

yes, I also think that 304 is correct one!!

1
1
how?
0
0
1 vote
1 vote

Expression is x+=f1()+f2()+f3()+f1();

if we expand this expression it look like below 

x=x+ f1()+f2()+f3()+f1();  here the tricky part actually, x=x+ (some value)

static int x =10 

x = x + f1() +f2() + f3() + f1()

f1() -->26 

x = x + 26 +f2() + f3() + f1()

f2() -->51

x = x + 26 + 51 + f3() + f1()

f3() -->100 // makes global "x =100" (imp point to note) , not static value inside the main method

x = x + 26 +51 + 100 + f1()

f1() -->27

x = x + 26 + 51 + 100 + 27

Now it's time to choose the x value in main method (variable scope) , it will consider the static  variable inside the main method 

so it's value is still 10 (x=10)

x = 10+ 26 +51 + 100 + 27 = 214

Answer must be 214.

if still not able understand the flow , just delete the static declaration inside the main method and execute the program , you will see the difference and what's scope of static variable inside the main method.

1 comment

 @HanuamntappaBudihal But  c compiler uses static scoping. How will the running of program will makes any difference?

0
0

Related questions