Thursday, 15 August 2013

chapter 6 solutions problem [A][d]

Question-
main( ) 

int x, y, s = 2 ; 
s *= 3 ; 
y = f ( s ) ; 
x = g ( s ) ; 
printf ( "\n%d %d %d", s, y, x ) ; 

int t = 8 ; 
f ( int a ) 

a += -5 ; 
t -= 4 ; 
return ( a + t ) ; 

g ( int a ) 

a = 1 ; 
t += a ; 
return ( a + t ) ; 
}

Solution-

little bit tricky. t is a global variable.value of s would become s=2*3=6,after that function call would pass s=6 which will be collected as 'a',so now a=6. in function f , operation 1 would make a=a-5=1; operation 2 would make t=8-4=4. so (a+t)=5. would be returned .so y=5, now in function g operation 1 would make a=1.now value t is 4 . Recall that we made change in the value of t in the function. so, the global value of t became 4 at that time. now operation 2 of function g would give t=4+1=5, and finally a+t=5+1=6 would returned as the value of x.
so finally printf statement would result s=6,y=5,x=6.

3 comments: