Sequence points in the C language

Source: Internet
Author: User

Sequence points are some of the special points in a program execution sequence. When a sequence point exists, the expression preceding the sequence point must be evaluated and the side effects have occurred before the expression and its side effects are evaluated after the sequence point.

What are side effects? To illustrate.

int a = 5;int b = a ++;

In the statement that assigns a value to b , the expression a++ has a side effect, and it returns the a current value of 5, a adding 1 to the action.

Which symbols generate sequence points?

" ,The sequence point is generated.

" , " is used to stitch multiple statements together into a single statement. For example:

int b = 5;++ b;

Can be stitched together by " , "

int b = 5, ++b;

Because " , " produces sequence points, the , expression on the left side of "" must be evaluated first, and if there are side effects, the side effects will take effect. The expression to the right of "" is then processed , .

&&And ||will produce sequence points

Logic and && and logical OR || generate sequence points.

Because the && short-circuit operation is supported, the expression on the left must be evaluated first, and && if the result is false , the right expression is no longer evaluated && and returned directly false .

||and && similar.

?:In the " ?"will produce sequence points

ternary operator ?: The " ? " in will produce a sequence point. Such as:

int a = 5;int b = a++ > 5? 0 : a;

bWhat is the result? Because " ? " has a sequence point, its left expression must be evaluated first. a++ > 5when compared to 5, a there is no self-increment, so an expression evaluates to false . Because the ? sequence point at "", the side effect of its left expression also takes effect immediately, that is, a increment by 1 and become 6. Because the ? expression on the left side evaluates to false , the ternary operator ?: returns : the value to the right a . The value at this time a is 6, so b the value is 6.

Order of execution between sequence points

The example given in the strange C code.

int i = 3;int ans = (++i)+(++i)+(++i);

(++i)+(++i)+(++i)There are no sequence points between them, what is the order of execution? After gcc compiles, it executes two ++i , adds them up, calculates the third, and ++i then adds them. And after the Microsoft VC + + compiles, executes three first ++i , then adds. The results are different, who is right and who is wrong?

No one is wrong. The C standard stipulates that the order of execution between two sequence points is arbitrary . Of course, this arbitrary is not against the operator precedence and binding characteristics of the premise. The meaning of this rule is to leave space for compiler optimizations.

Knowing this rule, we should avoid repeating the same variable that is incremented in one line of code, because the compiler behaves unpredictable. Imagine if you (++i)+(++i)+(++i) replace (++a)+(++b)+(++c) (where, and a b c are different variables), regardless ++a , ++b and ++c the evaluation order who first who, the results will be consistent.

Original: http://www.cnblogs.com/jiqingwu/p/c_sequence_point.html

Sequence points in the C language

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.