Hello, I’m Yue Chuang.

A variable of type integer plus 1 can be written like this:

a = a + 1;
Copy the code

or

a += 1;
Copy the code

However, C also supports a more concise way of writing this:

a++;
Copy the code

or

++a;
Copy the code

This is called self-addition or self-increment, and the clear meaning is to add one to itself every time.

And correspondingly, there’s a and a, and they’re called decrement, so they subtract 1 from themselves.

++ and — are called increment and decrement operators, respectively, and are used frequently in loop constructs.

Examples of increment and decrement:

#include <stdio.h>
int main(a)
{
    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);
    return 0;
}

// out
a=10, b=20
a=11, b=19
a=12, b=18
Copy the code

After the increment and decrement are complete, the old value is replaced with the new value, and the new value is stored in the current variable.

The result of increment and decrement must be received by a variable, so increment and decrement can only be done with variables, not numbers, for example10 + +Is wrong.

It is important to note that there is a difference between ++ before and after a variable:

  • ++In front is called pre-increment (for example ++a). The pre-autoincrement operation is performed before any other operation is performed.
  • ++After is called postincrement (e.g. A++). After self – increment, other operations are performed before self – increment.

Self – reduction (-) is the same, there are before self – reduction and self – reduction.

The following example better illustrates the difference between preautoincrement (preautodecrement) and postautoincrement (postautodecrement) :

#include <stdio.h>
int main(a)
{
    int a = 10, b = 20, c = 30, d = 40;
    int a1 = ++a, b1 = b++, c1 = --c, 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);
   
    return 0;
}

// out
a=11, a1=11
b=21, b1=20
c=29, c1=29
d=39, d1=40
Copy the code

I believe you have no doubt about the output results of A, B, C and D. The following focuses on the analysis of A1, B1, C1 and D1:

  1. For A1 =++a, ++a is executed first, resulting in 11, and then 11 is assigned to A1, so the final value of A1 is 11. And as a goes up, it’s going to be equal to 11.

  2. If b1 is equal to B ++, instead of increasing b by 1 right away, we give b1 the original value of b, and then we increase it by 1. B was equal to 20, so b1 is equal to 20. And b, as it increases, is going to be 21.

  3. For c1=–c, execute –c first, resulting in 29, then assign 29 to c1, so the final value of C1 is 29. And c is going to be 29.

  4. If d1 is equal to d– you don’t immediately subtract 1 from d, you give d1 the original value of d, and then you subtract 1. D was 40, so d1 is going to be 40. And d is going to be 39.

  • It can be seen that:a1=++a;It’s going to increment and then assign;
  • whileb1=b++;It will assign and then increment.c1=--c;d1=d--;And so it is.

To strengthen memory, let’s look at a composite example of increasing and decreasing:

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

// out
c=11, d=14
Copy the code

Let’s break it down:

  1. When executing statement 1, a-b is calculated first, and the result is 11. Then b is subtracted again, and the result becomes 0. And then we give c the result of a minus b, which is 11, so the value of C is 11.

  2. Before statement ② is executed, the value of b has already changed to 0. So if d is equal to (++a)-(–b), a is going to increase to 13, and then b is going to decrease to -1, and then 13-(-1) is going to be 14, so d is going to end up being 14.

Let me add another example of a C++ program to the reader:

Example 1:

#include <iostream>
using namespace std;
 
int main(a)
{
   int a = 21;
   int c ;
 
   // The value of a does not increment before assignment
   c = a++;   
   cout << "Line 1 - Value of a++ is :" << c << endl ;
 
   // After the expression is evaluated, the value of a is increased by 1
   cout << "Line 2 - Value of a is :" << a << endl ;
 
   // The value of a increases before the assignment
   c = ++a;  
   cout << "Line 3 - Value of ++a is :" << c << endl ;
   return 0;
}

// out
Line 1 - Value of a++ is :21
Line 2 - Value of a is :22
Line 3 - Value of ++a is  :23
Copy the code

Example 2:

#include <iostream>
using namespace std;


int main(a)

{
    int c;
    int d = 10;   // The test increases and decreases
    c = ++d;      // ++d: increments the value of d by 1, and then executes the command using the value of d
    cout << "D is equal to" << d << endl;
    cout << C is equal to "" << c << endl ;

    int e = 10;   // The test increases and decreases
    c = e++;      // e++ is to use the value of e to execute the command first, and then add 1 to the value of e
    cout << "E is equal to" << e << endl;
    cout << C is equal to "" << c << endl ;

    return 0;
}

// outD is equal to the11C is equal to the11E is equal to the11C is equal to the10
Copy the code

Both increment and decrement operators operate only on variable lvalues, not expressions, as follows:

#include<iostream>
using namespace std;
int main(a)
{
    int m = 3, n = 9, x;
    x = (m+n)++;        // Error, increment operator cannot apply to expression
    x = m++;            // Correct, the output is 3
    cout << x << endl;
    x = ++n;            // Correct, the output is 10
    cout << x << endl;
    return 0;
}
Copy the code