Let’s talk about self-increment first. Self-increment is divided into front self-increment and back self-increment
Front self-increment: ++i
Back self-increment: i++
#include<stdio.h>
int main()
{
int i,j,k,m;
i = j = 6;
k = ++i;
m = j++;
printf("i = %d,j = %d,k = %d,m = %d\n",i,j,k,m);
return 0;
}
The output is: i = 7, j = 7, k = 7, m = 6
- The value of the self-increasing overall expression before is the value after
i
is added by 1. My personal understanding is thati
first adds 1 to itself and then displays it. Assign to variablek
- The value of the auto-incrementing overall expression after is the value before
i
is added by 1. My personal understanding is that it first presents its original value and assigns it to variable m,i
Add 1 again - The front auto-increment and the back auto-increment will eventually increase the value of
i
by 1
In the same way, the former self-decrement and the rear self-decrement in self-decrement can be understood as described above.
Replenish
-
Using auto-increment and self-decrement can make the code more concise, and more importantly, the operation of the two modes of auto-increment and self-decrement
i=i+1
andi+=1
Much faster. When running the two modesi=i+1
andi+=1
, the computer first takes out the data in the memory and uses in the memory, and and are directly in the cpu Processed in the register.i
i
i++
++i
-
When using auto-increment and auto-decrement, it is best to make it a separate statement and try to avoid using it as part of a complete compound statement.