=============================================================================================================== =
int i = 0, sum;
Test unary operators
If printf ("%d", I); The result is: 0
If printf ("%d", i++); The result is: 0
If printf ("%d", ++i); The result is: 1
----------------------------------------------------------------------
int i = 0, sum;
Testing the two-dollar operator
sum = i++;
printf ("%d", sum);
printf ("%d", I);
The result is: 0 and 1
If you change to:
i = i++;
printf ("%d", I);
The result is: 1
----------------------------------------------------------------------
int a = 0, sum;
sum = a++ + a++;
printf ("%d\n", sum);
printf ("%d\n", a);
The result is: 0 and 2
Analysis:
First a++ is executed (at this point, the value of a is 0),
Then execute the second a++ (at which point A has a value of 0),
Finally, the addition and assignment operations are performed (at this point a value of 0,sum is 0 + 0 = 0),
After the addition operation is finished, perform a two plus action (at which point A is 2)
If you change to:
A = a++ + a++;
printf ("%d", a);
The result is: 2
----------------------------------------------------------------------
int a = 0,sum;
A = a++ + ++a;
printf ("%d\n", a);
Process:
First, a self-increment of 1, which is brought by ++a (even if a++ on the far left). At this point a = 1
We then calculate the two operand of the + sign.
Because they are actually a (this involves the compiler implementation problem, most C compilers will combine the two operands, that is, the variable a),
The value is 1, so that the return value of a++ + ++a is 1+1=2, and then the value of a++ + ++a is assigned to a, when a = 2
Finally, complete the self-increment instruction brought by a++, at which point A is the value of 2+1=3, which is a = 3
The result is: 3
If you change to:
sum = a++ + ++a;
printf ("%d", sum);
printf ("%d", a);
Process:
First, a self-increment of 1, which is brought by ++a (even if a++ on the far left). At this point a = 1
Then, we calculate the + number of the two operands.
Because they are actually a (this involves the compiler implementation problem, most C compilers will combine the two operands, that is, the variable a),
The value is 1, so that the return value of a++ + ++a is 1+1=2, and then the value of a++ + ++a is assigned to sum, when sum = 2,a = 1
Finally, complete the self-increment instruction brought by a++, at which point A is the value of 1+1=2, which is a = 2
Results are: 2 and 2
=============================================================================================================== =