The type of assignment expression in C/C++, like (i = j) = k;

int i = 1;
int j = 2;
int k = 3;
(i
= j) = k;

 

这段代码会有什么结果?

在C++中, i 的值是3

在C中,这是一个编译错误: '=' : left operand must be l-value

 

参看他们的标准

In ISO C++ 2003, 5.17 Assignment operators

1 There are several assignment operators, all of which group right-to-left. All require a modifiable lvalue as their left operand, and the type of an assignment expression is that of its left operand. The result of the assignment operation is the value stored in the left operand after the assignment has taken place; the result is an lvalue.
 

In C99 6.5.16 Assignment operators
3 An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue. The type of an assignment expression is the type of the left operand unless the left operand has qualified type, in which case it is the unqualified version of the type of the left operand. The side effect of updating the stored value of the left operand shall occur between the previous and the next sequence point.

4 The order of evaluation of the operands is unspecified. If an attempt is made to modify the result of an assignment operator or to access it after the next sequence point, the behavior is undefined.

 

posted @ 2010-12-19 11:10  嗷嗷  阅读(503)  评论(1编辑  收藏  举报