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

本文探讨了在C++和C语言中复杂赋值表达式的处理方式及其差异,特别是针对形如(i=j)=k这样的表达式。在C++中,最终i的值为3;而在C语言中,这将导致编译错误。

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.

 

转载于:https://www.cnblogs.com/aoaoblogs/archive/2010/12/19/1910410.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值