Reputation: 151
#include<stdio.h>
#include<conio.h>
int t=8;
int dok(int);
int doky(int);
int main()
{
int clrscr();
int x,y;
int s=2;
s*=3;
x=dok(s);
y=doky(s);
printf("%d%d%d",s,y,x);
getch();
return 0;
}
int dok(int a)
{
a+=-5;
t-=4;
return(a+t);
}
int doky(int a)
{
a=1;
t+=a;
return(a+t);
}
Answer to above code: 665
I understand why s=6
, x=1+4=5
(a=6-5=1
,t=8-4=4
)... Please tell me how y
comes as 6
, I thought y
would be 1+4=5
(a=1
, t=4
)
Thanks, please help me.
Upvotes: 0
Views: 119
Reputation: 108988
Just do it with pencil and paper ...
| t | x | y | s | a | -----------------+---+---+---+---+---+ before main | 8 |#NA|#NA|#NA|#NA| before x=dok(s) | 8 | ? | ? | 6 |#NA| inside dok | 4 |#NA|#NA|#NA| 1 | after dok | 4 | 5 | ? | 6 |#NA| before y=doky(s) | 4 | 5 | ? | 6 |#NA| inside doky | 5 |#NA|#NA|#NA| 1 | after doky | 5 | 5 | 6 | 6 |#NA|
Upvotes: 0
Reputation: 2650
The value of t is changed to 4 with the dok function, and the doky function increments that value by 1 (the value in a). Sum that (5 so far) to the value of a again (set to 1), and that's 4+1+1 = 6.
//t is 4, the value of a is irrelevant since it changes on the next instruction.
a=1;
t+=a; // t is now 5
return(a+t); // 1+5 = 6
Upvotes: 1
Reputation: 34625
tell me how y comes as 6 ...
Call to dok
function modifies t
to 4.
int doky(int a)
{
a=1;
t+=a; // Previously t is 4 because of t-=4 in earlier function call
// t = 4+1 = 5
return(a+t); // 1+5 = 6 retured
}
Upvotes: 5
Reputation: 3706
first t increases by a and then sum of a and t is returned
so, t was 4. then operator t += a is executed and t becomes 5. and a+t == 1+5 == 6 is returned
Upvotes: 1