[問題] 為什麼答案會這樣??? 請幫忙解釋一下

看板C_and_CPP (C/C++)作者時間16年前 (2009/11/10 00:35), 編輯推噓2(201)
留言3則, 2人參與, 最新討論串1/2 (看更多)
#include <stdio.h> int x = 2, y; int useStatic (void); void useGlobal(void); int main(void ) { int x = 5; y = 1; printf("%d %d\n", x, y++); int y = useStatic(); { useGlobal(); printf("%d %d\n", x, y); } x = useStatic() + y; printf("%d %d\n", x, y); return 0; } int useStatic () { static int y; printf("%d %d\n", x, ++y); return y; } void useGlobal() { printf("%d %d\n", ++x, y); } 跑出來答案為 5 1 2 1 3 2 5 1 3 2 3 1 為什麼第3行y的答案是2 不是1嗎??? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 203.72.84.170

11/10 00:39, , 1F
y = 1; printf("%d %d\n", x, y++); // thus y is 2
11/10 00:39, 1F

11/10 00:47, , 2F
可是useStatic()不是return 1?int y=1? 對不起有點轉不過來
11/10 00:47, 2F

11/10 01:06, , 3F
第三行是useGlobal()
11/10 01:06, 3F
文章代碼(AID): #1A-4J0mU (C_and_CPP)
文章代碼(AID): #1A-4J0mU (C_and_CPP)