The role of sequence points in the evaluation of C-language expressions

Source: Internet
Author: User

Summary:

This paper is a pioneering analysis of the role of sequence points in the evaluation of C-language expressions: The operand to the left of the sequence point is evaluated before its right operand. The comma operator, logic and operator &&, logic, or operator are discussed | | and the conditional operator?: The reason for the sequence point is required at the question mark. This paper analyzes the function of sequence points in evaluation of expressions in an example.

Keyword: sequence-point expression evaluation C language

C language as a mainstream programming language, many programming languages such as Java, C + +, C # have borrowed its syntax. C language is also a very appropriate introduction to the teaching of programming language, many colleges and universities in China have opened this course and most of it as the first programming language courses, while the C language is also the national Computer level test two level content. Therefore, the teaching quality of C language is very important for students ' follow-up study and obtaining grade certificate, especially for students to know and use the computer better in practical work.

At present, most of the textbooks in the country introduce the C language expression, there is not an important concept of C language-sequence point [1]. The absence of the concept of sequence points allows the reader to only memorize and not follow the evaluation rules to analyze the order of evaluation of sequence point expressions. With a simple comma expression of a = 3, ++a as an example, set the variable A to be an integer variable (the variable that appears in the expression below also defaults to the integer type). The comma expression is evaluated from left to right, so the child expression A = 3 is evaluated first, and then the pair expression ++a evaluated. The students who are good at questioning in practical teaching tend to ask: the increment operator + + in the expression has the highest priority, why should we first seek the value of the subexpression a=3? is the expression evaluated as an operator that is not a high-priority first? Is there an evaluation principle for expressions? This not only makes students ' knowledge of C language incomplete, but also influences the enthusiasm of students ' self-study, which is not conducive to the cultivation of innovative talents.

Definition and analysis of sequence points

According to the C language standard [2], the sequence point is a specific point in the execution sequence, at which the side effect of the front evaluation (side effect) should be completed completely and the side effect of subsequent evaluation does not occur. It is unwise for beginners to learn to understand sequence points by copying the standard in the textbook, and to point out the role of sequence points in the evaluation of expressions directly to beginners. When evaluating an expression that contains a sequence point, make sure that the operand to the left of the operator of the sequence point is evaluated before the operand on its right (3). The principle of evaluation of C-language expressions is that the sequence points are considered first, and then evaluated according to the precedence and associativity of the operators.

Operands are operands of operators, such as the operand to the left of the addition operator in the expression 3+2 is 3, and the operand to the right is 2. In complex expressions, it is necessary to combine the precedence and associativity of an operator to determine the operand of an operator. For an expression 3+2*5, the operand to the left of the addition operator is 3, but its right operand is not 2, and 3 of the addition operation is obviously impossible and 2 added. Because the operator that is adjacent to the right of the addition operator is the multiplication operator, its precedence is higher, so the operand to the right of the addition operator is 2*5 (the product). The number of operands to the left of the multiplication operator is 2 and the right operand is 5. For the expression 3+2-5, the operator on the right side of the addition operator is the subtraction operator with the same precedence, but the binding is left associative, so its right operand is 2.

Expression A = 0 && ++a The operand to the left is 0, the right operand is a subexpression ++a, the entire expression is an assignment expression, and the expression (A = 0) && ++a in the logical and left operand is a subexpression (a = 0), The operand to the right is the subexpression ++a, and the entire expression is a logical expression. The logic and operator have sequence points, so the expression (a = 0) && ++a is evaluated, although the increment operator has the highest precedence, but when the value is evaluated, the sequence point is considered first, and the operand to the left of the operator (A = 0) is evaluated before its right operand ++a.

The reason why some operators need sequence points in C language

The comma operator (,) has a sequence point. The comma operator is used to turn multiple statements into a single statement, such as a = 2, and ++a, two statements, and a = 2, ++a; is a statement. Statement A = 2, ++a; When executed, if the comma operator does not have a sequence point, the subexpression ++a executes first, that is, the statement executes in a different order than the two statements above. Based on the function of the comma operator, the comma operator can only have the lowest priority and contain sequence points.

The logic and operators (&&) have sequence points. C language in the logic and operator "short-circuit calculation", that is, when the left operation value of 0 is false, the right operand is not evaluated directly to the value of 0 (false) as the final result of evaluation. If the logic and operator do not have sequence points, the increment operator has the highest precedence when the expression 3>5 && ++a evaluates, and the subexpression ++a should be evaluated first. The operand to the left of the logic and operator 3>5 evaluates to 0, which is false, according to the short-circuit calculation, as the logical and right operand, sub-expression ++a will not be evaluated. Obviously the two contradictions. For short-circuit calculations, logic and operators && need sequence points. After the sequence point is evaluated, the operand to the left of the sequence point is evaluated before its right operand, that is, the subexpression is evaluated first, and because of the short-circuit calculation, the value of the entire expression is 0, which is false, and the right-operand subexpression ++a is not evaluated.

Logical OR operator (| | ) has a sequence point. The logic or operator in C also has a "short circuit calculation", so the necessity of having a sequence point is the same as the logic and operator.

Conditional operator?: The question mark at the point? There are sequence points. The conditional action constant is used to rewrite a simple if-else selection structure, such as the following statement

if (a > B)

++a;

Else

++b;

Can the conditional operator be rewritten to a > b? ++a: ++b;.

If the conditional operator does not have a sequence point, statements a > B? ++a: ++b; When executed, ++a and ++b are executed before the sub-expressions a > B, so that the order of execution is obviously different from the IF-ELSE selection structure, so the conditional operator?: The question mark? has a sequence point. Statement a > B? ++a: ++b; execution, question mark? The left operand a > B executes first, the value is true, the ++a is evaluated, the ++B is not evaluated, the value is false, and vice versa.

Evaluation analysis of expressions containing sequence points

Set integer variable A with a value of 0. For an expression ' a ' | | (A = 1) && (A + = 2), logic or | | The left operand is ' a ', the right operand is (a = 1) && (A + = 2), the left operand of logic and && is (a = 1), and the right operand is (a + = 2). When evaluating a sequence point first, the value of the logical or left operand ' a ' is true, the short-circuit calculation is performed, and the operand to the right is not evaluated, so the value of the entire expression is 1, which is true. During the evaluation of an expression, the value of variable a does not change.

For an expression (a = 0) && (a = 5) | | (A + = 1), the operand to the left of Logic and && is (a = 0), the right operand is (a = 5); Logical OR | | The operand to the left is (a = 0) && (a = 5), and the right operand is (a + = 1). When evaluating a sequence point, the logic and && left operand (a = 0) evaluates first, the value of variable A on the one hand becomes 0, on the other hand, the value of the subexpression is 0 is false, performs a short-circuit calculation, the operand to the right (A = 5) is not evaluated, so the original expression becomes 0 | | (A + = 1). Logical OR | | The left operation value is 0, that is false, can not short-circuit calculation, only the right operand to the value of the number. subexpression (A + = 1) evaluates when the value of variable A is changed from 0 to 1, on the other hand, the value of this subexpression is 1 is true, therefore, the value of the original expression is 1, that is true. When the evaluation of an expression is complete, the value of variable a becomes 1.

In particular, the use of an assignment expression as the operand of a logical operator is only to clearly indicate whether a sub-expression is evaluated during the evaluation of an expression, the practicality and readability of such an expression is very poor, and it is best not to use an assignment expression to do the operand in actual programming.

Sequence points not only allow low-priority operations to be Mr. Foo to high-priority operators, but also affect the associativity of operators. Expressions a > B? ++a:c > d? ++c: ++d has two conditional operators, the conditional operator is right associative, so the original expression should be evaluated in the order of (a > B? ++a: (C > d? ++c: ++d)). The 3 operands of the left conditional operator are sub-expressions a > B, sub-expressions ++a and subexpression C > d? ++c: ++d. According to associativity, sub-expressions (C > d? ++c: ++d) should be evaluated first, but because of the left-hand conditional operator's question mark, the subexpression a >b evaluates first, and if it is true, only the subexpression is ++a evaluated, not the subexpression C > d? ++c: ++d was evaluated. Obviously, the right combination of the conditional operator in this expression is used only to confirm the right operand of the left conditional operator, not to make the right condition operation Mr. Foo evaluated.

Iv. concluding remarks

Although there are many kinds of expressions in C language, there are relatively definite rules of expression evaluation, and correctly analyzing the order of evaluation of expressions according to the rules of evaluation can help stimulate students ' learning initiative. The concept of copying the sequence points in the standard of C language in the textbook is bound to increase the difficulty of learning, to provide beginners with an accurate and easy to understand concept, not only will not cause the C language knowledge points of incomplete, but also for the heuristic teaching provides the possibility.

Reference documents

[1] Huangying, Huri, Zhu Qishen. Sequence point--c a universal blind area of language teaching [J]. Computer Science and Technology, 2011 (3): 2105-2106.

[2] State Bureau of Technical Supervision. GB/T 15272-1994 programming language C[s]. Beijing: China Standard Press, 2004.

[3] Tuesday strong. C Language Connotation tutorial [M]. China Railway Press, 2013:68.

Effect of Sequence point to Expression Evaluation in Language C

Abstract:

The effect of the sequence point to expression evaluation in Language C are 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 Operat or need sequence points. The role of the sequence point on expression evaluation was discussed by citing examples.

Keywords:sequence Point; expression evaluation; Language C


The role of sequence points in the evaluation of C-language expressions

Contact Us

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.