Return Value of the value assignment operator
1.
A = 1;
Cout <a ++;
Print: 1
Cout <++;
Print: 2
2.
A = 1;
C = (A * = 2) + (a + = 3 );
Result: A = 5, c = 10.
C = (a + = 3) + (A * = 2 );
Result: a = 8, c = 16
Explanation:
() The priority is greater than + and =, so assign values first, and then sum the two.
A * = 2 is a to 2 (change the value in the address of a), A + = 3, and then change a to 5. Therefore, c = 5 + 5;
Similarly, the second c = 16;
3.
++ A returns the address of a, and a ++ must return an address because it is a value assignment operator, but obviously it cannot be the address of, therefore, the compiler will write code to allocate a memory of the same size as a from the stack, copy the value of A to this temporary memory, and then return the address of this temporary memory. This temporary memory is allocated because of the need of the compiler and has nothing to do with the programmer. Therefore, the programmer should not or cannot write this temporary memory (because the compiler is responsible for compiling the code, if the programmer wants to access this memory, the compiler will report an error), but can read its value, which is also the main purpose of the return address. The following statement is correct:
(++ A) = a + = 34;
But (A ++) = a + = 34; an error will be reported during compilation, because the memory identified by the address returned by a ++ can only be processed by the compiler, programmers can only obtain their values.
A ++ means to first return the value of A, that is, the address of the temporary memory mentioned above, and then add the value of the variable. If multiple A ++ instances exist at the same time, a temporary memory must be allocated for each a ++ instance (note that c = (a + = 3) + (A * = 2 ); ), it will be a little bad, and a ++ means to first return the value of A. When is the value of? In VC, when the expression contains the suffix "++" or the suffix "-", only one temporary memory is allocated, then, all the suffixes "++" or the suffixes "-" return the address of the temporary memory. After all the values of other operators that can be computed are calculated, then, write the value of the corresponding variable to the temporary memory, calculate the value of the expression, and add or subtract one from the value of the corresponding variable.
Therefore, a = 1; C = (a ++) + (A ++); after execution, the value of C is 2, and the value of A is 3. And as follows:
A = 1; B = 1; C = (++ A) + (A ++) + (B * = A ++) + (A * = 2) + (A * = A ++ );
During execution, the temporary memory is allocated first. Because of the five "()", the calculation order is from left to right,
Calculates the value of ++ A and returns the address of a after adding one. The value of A is 2.
Calculates the value of a ++ and returns the address of the temporary memory. The value of A is still 2.
Calculates a ++ in B * = A ++ and returns the address of the temporary memory. The value of A is still 2.
Calculate "* =" in B * = A ++, write the value of a to the temporary memory, calculate the value of B as 2, and return the address of B.
Calculates the value of a * = 2 and returns the address of a. The value of A is 4.
Calculates a ++ in a * = A ++ and returns the address of the temporary memory. The value of A is still 4.
Calculate "* =" in a * = A ++, write the value of a to the temporary memory, and return the address of a. The value of A is 16.
Calculate the remaining "+". For calculation, write the value of a to the temporary memory. The value 16 + 16 + 2 + 16 + 16 is 66, and the value is written to C.
Calculate the plus one of the three A ++ accounts, and the value of a becomes 19.
The use of value assignment operators in expressions is not highly respected. Because it does not conform to the common mathematical expression habits, and the computing sequence is easy to confuse. If there are multiple "++" operators, it is best to separate the expressions, otherwise it will easily lead to incorrect calculation order and calculation errors. In addition, the computing order is not limited to the above a ++.