请解释输出

时间:2011-03-31 22:49:30

标签: c

#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);
}

回答上述代码:665

我理解为什么s=6x=1+4=5a=6-5=1t=8-4=4)...请告诉我y如何6,我认为y将是1+4=5a=1t=4

谢谢,请帮帮我。

5 个答案:

答案 0 :(得分:5)

  

告诉我你是怎么回事6 ...

致电dok功能会将t修改为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 :(得分:1)

首先t增加a,然后a和t的总和返回

所以,t为4.然后执行运算符t + = a,t变为5。 并返回a + t == 1 + 5 == 6

答案 2 :(得分:1)

使用dok函数将t的值更改为4,并且doky函数将该值递增1(a中的值)。将(5到目前为止)的值再次加到a的值(设置为1),即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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

用铅笔和纸做吧......

                 | 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|