在 C 语言中,前缀和后缀自增运算符的区别:
前缀自增和后缀自增都是内置的 一元运算符。一元意味着:该函数只有一个输入。"运算符"的意思是:"对变量进行修改"。
自增(++)和自减(--)内置的一元运算符会修改它们所附加的变量。如果您尝试针对常量或字面量使用这些一元运算符,则会出现错误。
在 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