今天我看到一個有趣的聲明與後增量和預增量。請考慮以下程序 -如果我在同一個語句中使用前增量和後增量會發生什麼?
#include <stdio.h>
int main(){
int x, z;
x = 5;
z = x++ - 5; // increase the value of x after the statement completed.
printf("%d\n", z); // So the value here is 0. Simple.
x = 5;
z = 5 - ++x; // increase the value of x before the statement completed.
printf("%d\n", z); // So the value is -1.
// But, for these lines below..
x = 5;
z = x++ - ++x; // **The interesting statement
printf("%d\n", z); // It prints 0
return 0;
}
實際上在那個有趣的聲明中發生了什麼?聲明完成後,增加後應該增加x的值。那麼首先x對於該語句仍然是5。並且在預先增加的情況下,秒x的值應該是6或7(不確定)。
爲什麼它給出0到z的值?是5 - 5或6 - 6?請解釋。
您將進入未定義的行爲,這實際上取決於編譯器如何處理它。 –
此外[這一個](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points)。 – jrok