1. Self-increasing operator (Increment Operator) The increment operator + + increases the value of the operand by 1. + + can be placed in front of the operand or in the back. For example: ++n; n++; The result of both statements is to increase n by 1, which can be said to be no different. The same effect is achieved using the following statement: n = n + 1; Despite the above two statements, there is no difference between + + front and rear. However, there is a difference between the + + front and the back-placement. For example: int n = 1, post, pre; Post = n++; Pre = ++n; For post = n++; This statement, the value of n is assigned to post, n is only increased by 1. That is, after the statement is executed, the value of the post is 1, and the value of N becomes 2. and pre = ++n; This statement, N, first increases by 1 and then assigns the value of the increment to the pre. In other words, after this statement is executed, the value of the pre is 3,n 3. As a result, if the + + predecessors, then the number of operations + + 1 First increase, and then participate in other operations; if the + + post, then the operands of + + will participate in other operations, then increase by 1. Strictly speaking, the value of the operand of the predecessor + + is increased by 1 before being used, and the value of the operand of the back + + is increased by 1 after being used. For example: int n = 5, post = 1, pre = 1; Pre = ++n + pre; //The pre is 7 after the end of the operation n = 5; Post = n++ + post; //The post is 6 after the end of the Operation 2. Self-subtraction operator (decrement Operator) Self-subtraction operator--causes the operand value to be reduced by 1. --can be placed either in front of the operand or in the back. For example: --n; n--; The self-subtraction operator is very similar to the self-increasing operator, except that the decrement operator causes the operand to be reduced by 1, while the self-increasing operator increases the operand by 1. For example: int n = 5, post = 1, pre = 1; Pre =--n + pre; //The pre is 5 after the end of the Operation n = 5; Post = n--+ post; //The post is 6 after the end of the Operation 3. Priority level The self-add and decrement operators have high precedence, and only the parentheses have higher precedence than them. therefore, n*m++; means n (m++); instead of (n * m) + +;. and (n * m) + +; Is wrong. Because the operands of + + and--can only be variable left values (modifiable lvalue), and N * m is not. Note that you should not confuse precedence with the order of values. For example: int x = 1, y = 2, z; Z = (x + y++) * 3; //After the end of the Operation Z is 9,y 3 To replace the above statement with a number: Z = (1 + 2) * 3; Y will increase by 1 only when the value of Y is used. The priority indicates that + + is used only for Y, not (x + y). The priority also indicates when the value of Y is used, but when the value of Y is increased by 1 is determined by the nature of the self-increasing operator. When y++ is part of an arithmetic expression, you can assume that it means "use the value of y first, then increment." Similarly, ++y represents "self increase first, then use the value after the increment". ========================================================================== The following is from the "C Language FAQ" original book: Steve Summit Translation: Zhu Qun, Sun Yun Http://c-faq-chn.sourceforge.net/ccfaq/index.html Http://www.eskimo.com/~scs/C-faq/top.html ========================================================================== 4.3 for code int i = 3; i = i++; Different compilers give different results, some are 3, some are 4, which is correct? There is no correct answer; This expression has no definition. See also question 3.1, 3.7 and 11.32. Also note that i++ and ++i are different from i+1. If you want to make I self 1, use i=i+1, i+=1, i++ or ++i, not any combination, see question 3.10. i = i++ 's behavior is undefined, but I just tested it on an ANSI-compliant compiler and got the results I wanted. In the face of undefined behavior, including the scope of the implementation definition behavior and uncertainty behavior, the compiler can do any implementation, including all your expected results. But it is unwise to rely on this realization. Attend questions 7.4, 11.31, 11.32 and 11.34. 4.2 using my compiler, the following code int i=7; printf ("%d\n", i++ * i++); Return 49? In any order, shouldn't you print out 56? Although the suffix self plus and suffix decrement operator + + and the -- output of its old values will not perform operations, but here the ' after ' is often misunderstood. there is no guarantee that the self-increase or decrement occurs immediately after the output variable's original value and before the other parts of the expression are evaluated. It is also not guaranteed that the update of a variable will occur at a time before the expression ' complete ' (in terms of ANSI C, before the next ' sequence point ', see problem 3.7). In this example, the compiler chooses to multiply the old values of the variables and then add them back to their own operation. The behavior of code that contains multiple, indeterminate side effects is always considered undefined. (In simple terms, "multiple indeterminate side effects" refers to any combination of self-subtraction and assignment operators that are used in the same expression to cause the same object to be modified two times or modified and then referenced.) This is a rough definition; The strict definition See question 3.7, ' undefined ' meaning see question 11.32. Don't even try to find out how these things are implemented in your compiler (this is the opposite of many of the C textbook-retarded exercises); As K&r wisely pointed out, "If you do not know how they are implemented on different machines , such ignorance may just help protect you." 4.7 How can I understand complex expressions? What is the ' sequence point '? A sequence point is a point in time (after the entire expression has been calculated or at the | |, &&,?: or comma operator, or before the function call), and the dust settles and all side effects are ensured to end. Ansi/iso C Standard is described in this way:
between the previous and next sequence points, the value saved by an object can only be modified once by the expression's calculation. And the previous value can only be used to determine the value that will be saved.
The second sentence is more puzzling. It says that if an object needs to be written in an expression, access to that object in the same expression should be limited to directly evaluating the value that will be written. This rule effectively limits the validity of an expression that only ensures that the variable is accessed before it is modified. For example i = i+1 is valid, while a[i] = i++ is illegal (see problem 3.1). See the question below 3.8. |