[英]please explain the output
#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=6
, x=1+4=5
( a=6-5=1
, t=8-4=4
)...請告訴我y
是6
怎么來的,我以為y
是1+4=5
( a=1
, t=4
)
謝謝,請幫幫我。
告訴我y是6點...
調用dok
函數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
}
首先t增加a,然后返回a和t的總和
因此,t為4。然后執行運算符t + = a,並且t變為5。返回a + t == 1 + 5 == 6
使用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
y = a + t = a + t + a = 1 + 4 + 1 = 6 :)
只是用鉛筆和紙做...
| 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|
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.