# Function of sequence point in C expression evaluation

Source: Internet
Author: User

Function of sequence point in C expression evaluation

Abstract:

This article first analyzes the role of a sequence point in the C language expression evaluation: the operand on the left of a sequence point must evaluate the value on the right of the sequence point. We have discussed the comma operator, and the logic and operator &, the logic or operator | and the condition operator? : The reason why sequence points are required at question marks. The function of sequence points in expression evaluation is analyzed as an example.

Keywords: sequence point expression evaluate C Language

C language is a mainstream programming language. Many programming languages such as Java, C ++, and C # Use its syntax. C language is also a suitable language for getting started with programming. Many majors in Chinese colleges and universities have offered this course and most of them use it as the first programming language course, at the same time, the C language is also the second-level content of the National Computer Rank Examination. Therefore, the quality of C language teaching is very important for students to learn and obtain level certificates in the future, especially for students to better understand and use computers in practical work.

At present, most Chinese textbooks do not introduce an important concept of C language when introducing C language expressions-sequence point [1]. The lack of the concept of sequence points allows the reader to only remember the order of evaluation of sequence points expressions instead of following the evaluation rules. Take a simple comma expression a = 3, ++ a as an example. Set variable a as an integer variable (the variables in the following expression are also integer by default ). The comma expression evaluates values from left to right. Therefore, evaluate the pair expression a = 3 first, and then the pair expression ++. In practice, students who are good at questioning tend to ask: The self-incrementing operator ++ has the highest priority in the expression. Why do we first need to evaluate the value of the subexpression a = 3? Isn't the expression a high-priority operator first? Does the expression have the evaluate principle? This not only makes students have incomplete awareness of C language knowledge, but also affects students' enthusiasm for autonomous learning, which is not conducive to the cultivation of innovative talents.

I. Sequence point definition and Analysis

According to the C language standard [2], sequence points are some specific points in the execution sequence. At these points, side effect) it should be completely completed and the secondary effect of the subsequent value has not occurred. It is unwise to copy the standards in the textbooks so that beginners can learn and understand the sequence points. It is necessary to directly point out the function of the sequence points in expression evaluation for beginners. When you evaluate an expression containing a sequence point, make sure that the operand on the left of the operator with a sequence point is evaluated before the operand on the right of the operator [3]. The principle of C language expression evaluation is: first consider the sequence points, and then evaluate the value based on the operator's priority and combination.

The operand refers to the operator's operation object. For example, the operand on the left of the normal Operator in expression 3 + 2 is 3, and the operand on the right is 2. In a complex expression, the operator's operands need to be determined based on the operator's priority and associativity. For expression 3 + 2*5, the operand on the left of the addition operator is 3, but the operand on the right is not 2. It is obviously impossible for addition 3 to 2. Because the operator adjacent to the right of the addition operator is a multiplication operator with a higher priority, the operand on the right of the addition operator is 2*5 (product ). The operand on the left side of the multiplication operator is 2, and the operand on the right is 5. For expressions 3 + 2-5, the adjacent operators on the right of the addition operator are the subtraction operator. The two operators have the same priority, but the combination is left. Therefore, the right operand of the addition operator is 2.

In expression a = 0 & ++ a, the operands on the logic and left are 0, the operands on the right are subexpressions + + a, and the entire expression is a value expression. The expressions (a = 0) in & ++ a, the operands on the logic and left are subexpressions (a = 0), the operands on the right are subexpressions + + a, and the entire expression is a logical expression. The logic and operator have a sequence point. Therefore, when the expression (a = 0) & ++ a is evaluated, although the auto-increment operator has the highest priority, the sequence point is first considered when the value is evaluated, the operand on the left of the logic and operator (a = 0) must be evaluated prior to the operand ++ a on the right.

Ii. reasons why some operators in the C language need sequence points

The comma operator (,) has a sequence point. The comma operator is used to convert multiple statements into one statement, for example, a = 2; and ++ a;. It is two statements, and a = 2, ++ a; is a statement. Statement a = 2, ++ a; if the comma operator does not have a sequence point during execution, the subexpression ++ a is executed first, that is, the execution sequence of this statement is different from that of the preceding two statements. Based on the comma operator, the comma operator can only have the lowest priority and contain sequence points.

The logic and operator (&) have sequence points. In C language, the logic and operators are implemented with "short-circuit calculation", that is, when the operation value on the left is 0, that is, false, 0 (false) is not directly calculated for the right operand) as the final result of the evaluation. If the logic and operator do not have a sequence point, when expression 3> 5 & ++ a is evaluated, the auto-increment operator has the highest priority. The sub-expression ++ a should first evaluate the value. The result of the operand 3> 5 on the left of the logic and operator is 0, that is, false. Based on Short-circuit calculation, as the operand of the logic and the right, the sub expression ++ a will not be evaluated. Obviously, the two are in conflict. For short-circuit computation, logic and operators & sequence points are required. After a sequence point is obtained, the operand on the left of the sequence point is evaluated prior to the operand on the right of the sequence point, that is, the substring 3> 5 is first evaluated. Due to short-circuit calculation, the value of the entire expression is 0, that is, false, and the right operand sub-expression ++ a is not evaluated.

The logic or operator (|) has a sequence point. In C language, the logic or operator also implements "short-circuit calculation". Therefore, the necessity of ITS sequence points is the same as that of its logic and operator.

Conditional operator? : Question mark? There are sequence points. Conditional operators are often used to rewrite the simple if-else selection structure. The following statements

If (a> B)

++;

Else

+ + B;

The available condition operator is changed to a> B? ++ A: ++ B ;.

If the condition operator does not have a sequence point, Statement a> B? ++ A: ++ B. During execution, ++ a and ++ B are executed prior to the subexpression a> B, the execution sequence is obviously different from the if-else selection structure. Therefore, the conditional operator? : Question mark? There is a sequence point. Statement a> B? ++ A: ++ B; where is the question mark during execution? The operations a> B on the left are executed first. When the value is true, the operations a> B evaluate the value of ++ a. If the value is false, the Operations B.

Iii. Evaluation Analysis of expressions containing sequence points

Set the value of integer variable a to 0. For expressions 'A' | (a = 1) & (a + = 2), logical or | the left operand is 'A ', the right operand is (a = 1) & (a + = 2); the logical and & the left operand is (a = 1 ), the right operand is (a + = 2 ). When calculating the value, first consider the sequence point. If the value of the logical or left operand 'A' is not "0", the right operand of the logical or left operand is not evaluated. Therefore, the value of the entire expression is 1, that is, true. The value of variable a does not change during expression evaluation.

For expressions (a = 0) & (a = 5) | (a + = 1), The operands of logic and & on the left are (a = 0 ), the right operand is (a = 5); logic or | the left operand is (a = 0) & (a = 5 ), the right operand is (a + = 1 ). When you evaluate a value, first consider the sequence point. The logic is evaluated first with the & left operand (a = 0). When you evaluate a value, the value of variable a is changed to 0, on the other hand, if the value of this subexpression is 0, it is false. When short-circuit computation is executed, the right operand (a = 5) is not evaluated. Therefore, the original expression is 0 | (a + = 1 ). Logic or | the operation value on the left is 0, that is, false. Short-circuit computation is not allowed. The value of the right operand can only be obtained. When a sub-expression (a + = 1) is evaluated, on the one hand, the value of variable a is changed from 0 to 1, on the other hand, the value of this Sub-expression is 1, that is, true. Therefore, the value of the original expression is 1, that is, true. After the expression is evaluated, the value of variable a is changed to 1.

It is particularly emphasized that the operands using the value assignment expression as logical operators only need to clearly indicate whether a subexpression is evaluated during the expression evaluate process. This type of expression has very poor practicability and readability, in actual programming, it is best not to use a value expression as the operand.

Sequence points not only allow low-priority operators to evaluate before high-priority operators, but also affect the Union of operators. Expression a> B? ++ A: c> d? ++ C: ++ d has two condition operators. The condition operator is a combination of the right. Therefore, the order in which the original expression evaluates is (a> B? ++ A: (c> d? ++ C: ++ d )). The three operands of the left-side conditional operators are subexpressions a> B, subexpressions ++ a, and subexpressions c> d? ++ C: ++ d. Subexpressions (c> d? ++ C: ++ d) first evaluate the value. However, due to the question mark of the left-side conditional operator? There is a sequence point, so the subexpression a> B first evaluates the value. If it is true, only the subexpression + + a evaluates the value, instead of the subexpression c> d? ++ C: ++ d. Obviously, the right combination of condition operators in this expression is only used to confirm the right operand of the Left conditional operator, without making the right conditional operator evaluate first.

Iv. Conclusion

Although there are many expressions in C language, there are relatively clear expression evaluation rules. correct analysis of the order of expression evaluation based on the evaluation rules helps to stimulate students' initiative in learning. Copying the concept of sequence points in the C language standards in teaching materials will inevitably increase the difficulty of learning, and provide beginners with an accurate and easy-to-understand concept, which will not only lead to incomplete knowledge points in the C language, it also provides the possibility of heuristic teaching.

References

[1] Huang Yu, Hu lie, Zhu qishen. Sequence point-a common blind zone in C language teaching [J]. Computer Knowledge and Technology, 2011 (3): 2105-2106.

[2] State Bureau of Technical Supervision. GB/T 15272-1994 Programming Language C [S]. Beijing: China Standard Publishing House, 2004.

[3] Tuesday strong. C language connotation tutorial [M]. China Railway Publishing House, 2013: 68.

Effect of Sequence Point on Expression Evaluation in Language C

Abstract:

The effect of the sequence point on expression evaluation in Language C is analyzed from all new angle of view. the operand in the form of sub-expression located to the left of the operator with sequence point is evaluated before the right operand. this text explains the reason the comma operator, logical AND operator, logical OR operator and ternary conditional operator need sequence points. the role of the sequence point on expression evaluation is discussed by citing examples.

Keywords: sequence point; expression evaluation; language C

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.