int get()
{
static i = 1;
return i++;
}
int main(int argc, char *argv[])
{
printf("%d %d %d\n", get(), get(), get());
return 0;
}
輸出:3 2 1(順序取決於編譯器)對於後增量
問:但是爲什麼前值增量返回的靜態變量(文件範圍)的。後/前增量的拇指規則是什麼?我從來沒有得到正確的。請幫忙。
好吧,讓我來更具體一些,我讀的所有例子都是這樣的表達式,然後賦值或賦值然後遞增。但是,這是什麼樣的表達式,return i++; func(a++);
我是這樣讀的:「在i ++之後沒有分配,因此返回最後遞增的值」(在此糾正)
order is undefined..but爲什麼值返回1? –
您需要閱讀[順序點](http://en.wikipedia.org/wiki/Sequence_point)。 – devnull
@codeymonkey閱讀http://stackoverflow.com/questions/17366847/what-is-the-difference-between-pre-increment-and-post-increment-in-the-cycle-fo –