C のプリインクリメントとポストインクリメントの違い:
プレインクリメントとポストインクリメントは、組み込みの単項演算子です。単項とは、「入力が 1 つの関数」を意味します。「演算子」とは、「変数に変更が加えられる」ことを意味します。
インクリメント (++) およびデクリメント (--) 組み込みの単項演算子は、それらが接続されている変数を変更します。これらの単項演算子を定数またはリテラルに対して使用しようとすると、エラーが発生します。
C では、すべての組み込み単項演算子のリストを次に示します。
Increment: ++x, x++
Decrement: −−x, x−−
Address: &x
Indirection: *x
Positive: +x
Negative: −x
Ones_complement: ~x
Logical_negation: !x
Sizeof: sizeof x, sizeof(type-name)
Cast: (type-name) cast-expression
これらの組み込み演算子は、変数の入力を受け取り、計算結果を同じ変数に戻す変装した関数です。
ポストインクリメントの例:
int x = 0; //variable x receives the value 0.
int y = 5; //variable y receives the value 5
x = y++; //variable x receives the value of y which is 5, then y
//is incremented to 6.
//Now x has the value 5 and y has the value 6.
//the ++ to the right of the variable means do the increment after the statement
プレインクリメントの例:
int x = 0; //variable x receives the value 0.
int y = 5; //variable y receives the value 5
x = ++y; //variable y is incremented to 6, then variable x receives
//the value of y which is 6.
//Now x has the value 6 and y has the value 6.
//the ++ to the left of the variable means do the increment before the statement
ポスト デクリメントの例:
int x = 0; //variable x receives the value 0.
int y = 5; //variable y receives the value 5
x = y--; //variable x receives the value of y which is 5, then y
//is decremented to 4.
//Now x has the value 5 and y has the value 4.
//the -- to the right of the variable means do the decrement after the statement
プレデクリメントの例:
int x = 0; //variable x receives the value 0.
int y = 5; //variable y receives the value 5
x = --y; //variable y is decremented to 4, then variable x receives
//the value of y which is 4.
//x has the value 4 and y has the value 4.
//the -- to the right of the variable means do the decrement before the statement