Cprogramming 简明教程

Increment and Decrement Operators in C

C - Increment and Decrement Operators

增量运算符 (++) 将变量值加 1,而减量运算符 (--) 则减 1。

The increment operator (++) increments the value of a variable by 1, while the decrement operator (--) decrements the value.

增量和减量运算符经常用于 C 语言中计数循环的构造(带有 for loop )。它们也在数组和 pointer arithmetic 的遍历中得到应用。

Increment and decrement operators are frequently used in the construction of counted loops in C (with the for loop). They also have their application in the traversal of array and pointer arithmetic.

++ 和 — 运算符是一元的,可作为变量的前缀或后缀使用。

The ++ and — operators are unary and can be used as a prefix or posfix to a variable.

Example of Increment and Decrement Operators

在下面这个示例中包含多个语句,展示了在不同变化下如何使用增量和减量运算符 −

The following example contains multiple statements demonstrating the use of increment and decrement operators with different variations −

#include <stdio.h>

int main() {
  int a = 5, b = 5, c = 5, d = 5;

  a++; // postfix increment
  ++b; // prefix increment
  c--; // postfix decrement
  --d; // prefix decrement

  printf("a = %d\n", a);
  printf("b = %d\n", b);
  printf("c = %d\n", c);
  printf("d = %d\n", d);

  return 0;
}

Output

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

a = 6
b = 6
c = 4
d = 4

Example Explanation

换句话说,“a” 的效果与 “a” 相同,因为这两个表达式都将变量 “a” 的值加 1。类似地,“a--” 的效果与 “--a” 相同。

In other words, "a" has the same effect as "a", as both the expressions increment the value of variable "a" by 1. Similarly, "a--" has the same effect as "--a".

表达式 “a++;” 可以等同于语句 “a = a + 1;”。在这里,右边的表达式将 1 加到 “a” 中,结果将重新赋值为 1,从而使 “a” 的值加 1。

The expression "a++;" can be treated as the equivalent of the statement "a = a + 1;". Here, the expression on the right adds 1 to "a" and the result is assigned back to 1, therby the value of "a" is incremented by 1.

类似地,“b--;” 等同于 “b = b – 1;”。

Similarly, "b--;" is equivalent to "b = b – 1;".

Types of Increment Operator

有两种类型的增量运算符 – pre incrementpost increment

There are two types of increment operators – pre increment and post increment.

Pre (Prefix) Increment Operator

在表达式中,前置增量运算符在使用变量值之前将变量的值加 1。

In an expression, the pre-increment operator increases the value of a variable by 1 before the use of the value of the variable.

++variable_name;

在下面的示例中,我们正在使用前置增量运算符,其中 “x” 的值将加 1,然后在表达式中使用这个加值。

In the following example, we are using a pre-increment operator, where the value of "x" will be increased by 1, and then the increased value will be used in the expression.

#include <stdio.h>

int main() {
  int x = 10;

  int y = 10 + ++x;

  printf("x = %d, y = %d\n", x, y);

  return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

x = 11, y = 21

Post (Postfix) Increment Operator

在表达式中,后置增量运算符在使用变量值之后将变量的值加 1。

In an expression, the post-increment operator increases the value of a variable by 1 after the use of the value of the variable.

variable_name++;

在下面的示例中,我们正在使用后置增量运算符,其中 “x” 的值将在表达式中使用,然后加 1。

In the following example, we are using post-increment operator, where the value of "x" will be used in the expression and then it will be increased by 1.

#include <stdio.h>

int main() {
  int x = 10;

  int y = 10 + x++;

  printf("x = %d, y = %d\n", x, y);

  return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

x = 11, y = 20

Types of Decrement Operator

有两种类型的减量运算符 – pre decrementpost decrement

There are two types of decrement operators – pre decrement and post decrement.

Pre (Prefix) decrement Operator

在表达式中,前置减量运算符在使用变量值之前将变量的值减 1。

In an expression, the pre-decrement operator decreases the value of a variable by 1 before the use of the value of the variable.

--variable_name;

在下面的示例中,我们正在使用前置减量运算符,其中 “x” 的值将减 1,然后在表达式中使用这个减值。

In the following example, we are using a pre-decrement operator, where the value of "x" will be decreased by 1, and then the decreased value will be used in the expression.

#include <stdio.h>

int main() {
  int x = 10;

  int y = 10 + --x;

  printf("x = %d, y = %d\n", x, y);

  return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

x = 9, y = 19

Post (Postfix) Decrement Operator

在表达式中,后置减量运算符在使用变量值之后将变量的值减 1。

In an expression, the post-decrement operator decreases the value of a variable by 1 after the use of the value of the variable.

variable_name--;

在下面的示例中,我们正在使用后置减量运算符,其中 “x” 的值将在表达式中使用,然后减 1。

In the following example, we are using post-decrement operator, where the value of "x" will be used in the expression and then it will be decreased by 1.

#include <stdio.h>

int main() {
  int x = 10;

  int y = 10 + x--;

  printf("x = %d, y = %d\n", x, y);

  return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

x = 9, y = 20

More Examples of Increment and Decrement Operators

Example 1

下面的示例重点介绍了前缀/后缀增量/减量的使用 −

The following example highlights the use of prefix/postfix increment/decrement −

#include <stdio.h>

int main(){

   char a = 'a', b = 'M';
   int x = 5, y = 23;

   printf("a: %c b: %c\n", a, b);

   a++;
   printf("postfix increment a: %c\n", a);

   ++b;
   printf("prefix increment b: %c\n", b);

   printf("x: %d y: %d\n", x, y);

   x--;
   printf("postfix decrement x : %d\n", x);

   --y;
   printf("prefix decrement y : %d\n", y);

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

a: a b: M
postfix increment a: b
prefix increment b: N

x: 5 y: 23
postfix decrement x: 4
prefix decrement y: 22

上面的示例表明前缀和后缀运算符对操作数变量的值具有相同的效果。但是,当在表达式中这些 “++” 或 “--” 运算符与其它运算符一起出现时,它们的行为不同。

The above example shows that the prefix as well as postfix operators have the same effect on the value of the operand variable. However, when these "++" or "--" operators appear along with the other operators in an expression, they behave differently.

Example 2

在以下代码中,“a”和“b” variables 的初始值相同,但 printf() function 显示的值不同−

In the following code, the initial values of "a" and "b" variables are same, but the printf() function displays different values −

#include <stdio.h>

int main(){

   int x = 5, y = 5;

   printf("x: %d y: %d\n", x,y);
   printf("postfix increment x: %d\n", x++);
   printf("prefix increment y: %d\n", ++y);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

x: 5 y: 5
postfix increment x: 5
prefix increment y: 6

在第一种情况下,printf() 函数打印“x”的值,然后对其值进行递增。在第二种情况下,首先执行递增运算符,printf() 函数使用递增的值进行打印。

In the first case, the printf() function prints the value of "x" and then increments its value. In the second case, the increment operator is executed first, the printf() function uses the incremented value for printing.

Operator Precedence of Increment and Decrement Operators

有很多种 operators in C 。当在一个表达式中使用多个运算符时,将按照其优先级顺序执行。增量和减量运算符与其他运算符一起使用时行为不同。

There are a number of operators in C. When multiple operators are used in an expression, they are executed as per their order of precedence. Increment and decrement operators behave differently when used along with other operators.

当一个表达式在其他运算符旁边包含增量或减量运算符时,将首先执行增量和减量操作。后缀增量和减量运算符的优先级高于前缀增量和减量运算符。

When an expression consists of increment or decrement operators alongside other operators, the increment and decrement operations are performed first. Postfix increment and decrement operators have higher precedence than prefix increment and decrement operators.

Example 1

请看以下示例:

Take a look at the following example −

#include <stdio.h>

int main(){

   int x = 5, z;

   printf("x: %d \n", x);

   z = x++;
   printf("x: %d z: %d\n", x, z);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

x: 5
x: 6 z: 5

由于“x++”将“x”的值增至 6,你可能会希望“z”也为 6。然而,结果显示“z”为 5。这是因为赋值运算符比后缀增量运算符具有更高的优先级。因此,“x”的现有值赋值给“z”,然后才递增“x”。

Since "x++" increments the value of "x" to 6, you would expect "z" to be 6 as well. However, the result shows "z" as 5. This is because the assignment operator has a higher precedence over postfix increment operator. Hence, the existing value of "x" is assigned to "z", before incrementing "x".

Example 2

再看下面另一个示例−

Take a look at another example below −

#include <stdio.h>

int main(){

   int x = 5, y = 5, z;

   printf("x: %d y: %d\n", x,y);

   z = ++y;
   printf("y: %d z: %d\n", y ,z);

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

y: 5
y: 6 z: 6

结果可能令人困惑,因为“y”和“z”的值现在都是 6。原因是前缀增量运算符比赋值运算符具有更高的优先级。因此,“y”首先被递增,然后其新值被赋值给“z”。

The result may be confusing, as the value of "y" as well as "z" is now 6. The reason is that the prefix increment operator has a higher precedence than the assignment operator. Hence, "y" is incremented first and then its new value is assigned to "z".

associativity of operators 也扮演着重要的角色。对于增量和减量运算符,关联性是从左到右。因此,如果一个表达式中有多个增量或减量运算符,则将首先执行最左边的运算符,向右移动。

The associativity of operators also plays an important part. For increment and decrement operators, the associativity is from left to right. Hence, if there are multiple increment or decrement operators in a single expression, the leftmost operator will be executed first, moving rightward.

Example 3

在此示例中,赋值表达式同时包含前缀和后缀运算符。

In this example, the assignment expression contains both the prefix as well as postfix operators.

#include <stdio.h>

int main(){

   int x = 5, y = 5, z;

   z = x++ + ++y;
   printf("x: %d y: %d z: %d\n", x,y,z);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

x: 6 y:6 z: 11

在此示例中,要完成的第一个操作是“y”(“y”变为 6)。其次,“+”运算符将“x”(为 5)和“y”相加,结果赋值给“z”为 11,然后“x”递增“x”为 6。

In this example, the first operation to be done is "y" ("y" becomes 6). Secondly the "+" operator adds "x" (which is 5) and "y", the result assigned to "z" as 11, and then "x" increments "x" to 6.

Using the Increment Operator in Loop

在 C 中, for 循环最常用的语法如下−

In C, the most commonly used syntax of a for loop is as follows −

for (init_val; final_val; increment) {
   statement(s);
}

Example

循环体对于变量的初始值和最终值之间的所有值执行,且在每一轮后递增该变量。

The looping body is executed for all the values of a variable between the initial and the final values, incrementing it after each round.

#include <stdio.h>

int main(){

   int x;

   for (x = 1; x <= 5; x++){
      printf("x: %d\n", x);
   }

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

x: 1
x: 2
x: 3
x: 4
x: 5