Home>

An integer plus one can be written like this:

a +=1;

It is equivalent toa=a + 1;.

But there is a simpler way of writing in C language,Is a ++;or ++ a ;. This writing is called auto-increment or auto-increment;The meaning is clear,It is one plus one.

Correspondingly, there are also a-- and --a, called self-decreasing,Means one minus itself.

++ and-are called increment and decrement operators respectively.

Examples of self-incrementing and decrementing:

#include<stdio.h>
#include<stdlib.h>
int main ()
{
 int a=10, b=20;
 printf ("a =%d, b =%d \ n", a, b);
 ++ a;
 --b;
 printf ("a =%d, b =%d \ n", a, b);
 a ++;
 b--;
 printf ("a =%d, b =%d \ n", a, b);
 system ("pause");
 return 0;
}

operation result:

a=10, b=20

a=11, b=19

a=12, b=18

After the self-incrementing and decrementing is completed,Will replace the old value with the new value,And save the new value in the current variable.Increment and decrement can only be applied to variables.Not for numbers,For example 10 ++ is an error

of.

It is worth noting that++ is different before and after the variable:

++ was previously called pre-increment (eg ++ a). Before the auto-increment operation,Do other operations.

++ is called post-increment (for example, a ++). After the increment, other operations are performed first.Then perform the auto-increment operation.

The same goes for self-decrement (-).

Consider the following example:

#include<stdio.h>
#include<stdlib.h>
int main ()
{
 int a=10, a1=++ a;
 int b=20, b1=b ++;
 int c=30, c1 =-c;
 int d=40, d1=d--;
 printf ("a =%d, a1 =%d \ n", a, a1);
 printf ("b =%d, b1 =%d \ n", b, b1);
 printf ("c =%d, c1 =%d \ n", c, c1);
 printf ("d =%d, d1 =%d \ n", d, d1);
 system ("pause");
 return 0;
}

Output results:

a=11, a1=11

b=21, b1=20

c=29, c1=29

d=39, d1=40

The output results of a, b, c, and d believe that everyone has no doubt.The following focuses on a1, b1, c1, and d1:

1) For a1=++ a, execute ++ a first, the result is 11, and then assign 11 to a1, so the final value of a1 is 11. And a has increased by itself,The final value is also 11.

2) For b1=b ++, the value of b is not immediately increased by 1, but the original value of b is first given to b1, and then increased by 1. The original value of b is 20, so the value of b1 is also 20. While b

After increasing,The final value is 21.

3) For c1 =-c, execute --c first, the result is 29, and then assign 29 to c1, so the final value of c1 is 29. And c is decremented,The final value is also 29.

4) For d1=d--, the value of d is not immediately reduced by 1, but the original value of d is first given to d1, and then decreased by 1. The original value of d is 40, so the value of d1 is also 40. And d

After decrementing, the final value is 39.

It can be seen that:a1=++ a;will be incremented first,Then perform the assignment operation;And b1=b ++;the assignment will be performed first,Then perform the auto-increment operation.c1 =-c;and d1=d--;also

So so.

It is very convenient to increase and decrease.It is often used in subsequent programming.We must pay attention to the distinction.

To strengthen memory,Let's look at a comprehensive example of self-incrementing and decrementing:

#include<stdio.h>
#include<stdlib.h>
int main ()
{
 int a=12;
 int b=1;
 int c=a- (b--);//①
 int d=(++ a)-(-b);//②
 printf ("c =%d, d =%d \ n", c, d);
 system ("pause");
 return 0;
}

Output results:

c=11, d=14

Let's analyze it:

1) When the statement ① is executed, the a-b operation is performed first, and the result is 11, and then b is decremented to become 0. Finally, the result of a-b (that is, 11) is passed to c, so c

The value is 11.

2) Before executing statement ②, the value of b has become 0. For d=(++ a)-(-b), a will increase first,Becomes 13, and then b decrements to -1, and finally 13-(-

1), the result is 14, and it is given to d, so d ends up being 14.

c
  • Previous Simple example of JS saving pictures in IE
  • Next C language double and float examples