0
#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.

5 Answers 5

5

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
}
1
  • Thanks !! i forgot the the addition of a in return statement.But now it is cleared. :)
    – John
    Mar 31, 2011 at 22:59
1

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

1

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
0

y = a + t = a + t + a = 1 + 4 + 1 = 6 :)

0

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|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.