设有定义:int x=2;,以下表达式中,值不为6的是______。 A. x*=x+1 B.
A. x*=x+1
B. 设有定义:int x=2;,以下表达式中,值不为6的是______。
A. x*=x+1
B. x++,2*x
C. x*=(1+x)
D. 2*x,x+=2 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
A. x*=x+1;//理由:*=优先级低于+,所以经过x*=x+1运算后表达式的值为9,不是6。
B. x++,2*x
C. x*=(1+x)
D. 2*x,x+=2
看上面的标准解释,我摘几句吧:
A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects(什么是副作用?网页链接) of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed.
In C and C++,sequence points occur in the following places. (In C++, overloaded operators act like functions, and thus operators that have been overloaded introduce sequence points in the same way as function calls.)
Between evaluation of the left and right operands of the && (logical AND), || (logical OR) (as part of short-circuit evaluation), and comma operators.
选项B就是 comma operator,即逗号操作符,逗号操作符会有evaluation sequence point(运算顺序点)。
++运算就是side-effect。顺序点就会要求其前面的所有运算都先算完,再执行后面的运算。
所以选项B等价于 "x+=1,2*x",结果是6.
2 * x 并没有改变x的值,