关于自加(++)的问题

无痕云海 2011-10-23 05:29:35
对于整型变量a有下面两个表达式:

1 (a++) += a;
2 (++a) += a;

两个表达式是否都正确,如果a=10,表达式的值为多少呢?

...全文
132 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
myhaikuotiankong 2011-10-23
  • 打赏
  • 举报
回复
++是不能做为左值的。。。
机智的呆呆 2011-10-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 frankhb1989 的回复:]
引用 4 楼 blue_shy_bruce 的回复:

引用 2 楼 demon__hunter 的回复:
在c++03中1错2未指定行为
c++11中1错2未定义行为

未定义行为可以理解为在不同的编译环境下可能会得到不同的结果吗?

ISO C99:
6.5.16 Assignment operators
Constraints
2 An assignment opera……
[/Quote]
受教了。
机智的呆呆 2011-10-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 frankhb1989 的回复:]
引用 4 楼 blue_shy_bruce 的回复:

引用 2 楼 demon__hunter 的回复:
在c++03中1错2未指定行为
c++11中1错2未定义行为

未定义行为可以理解为在不同的编译环境下可能会得到不同的结果吗?

ISO C99:
6.5.16 Assignment operators
Constraints
2 An assignment opera……
[/Quote]
受教了。
无痕云海 2011-10-23
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 youkic7 的回复:]
编译通过了吗
[/Quote]
用gcc的话,1和2都是编译错误的;但是用g++的话,1是错误的,2是可以的,结果是22.
无痕云海 2011-10-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wangdong20 的回复:]
1.相当于10+11=21
2.相当于11+11=22
这是我个人看法
[/Quote]
逻辑上也许可以这样理解,但是语法上好像有限制
youkic7 2011-10-23
  • 打赏
  • 举报
回复
编译通过了吗
wangdong20 2011-10-23
  • 打赏
  • 举报
回复
1.相当于10+11=21
2.相当于11+11=22
这是我个人看法
柯本 2011-10-23
  • 打赏
  • 举报
回复
又见日经帖
(a++)+=a;不可以,LS都说过了
(++a) += a; 结果如下

; a=10;
;
@1:
mov eax,10
;
; (++a) += a;
;
?live16385@32: ; EAX = a
inc eax //++a a=11
add eax,eax // a+=a a=22

无痕云海 2011-10-23
  • 打赏
  • 举报
回复
楼上讲的很详细,谢谢啦~~
FrankHB1989 2011-10-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 blue_shy_bruce 的回复:]

引用 3 楼 programmingring 的回复:
在C中自增操作不能作为左值

这个可以从某个角度进行理解吗,还是标准规定的?
[/Quote]
ISO C99:
6.5.2.4
Semantics
2 The result of the postfix ++ operator is the value of the operand. After the result is obtained, the value of the operand is incremented. (That is, the value 1 of the appropriate type is added to it.) See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers. The side effect of updating the stored value of the operand shall occur between the previous and the next sequence point.
6.5.2.1
Semantics
2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++E is equivalent to (E+=1). See the discussions of additive operators and compound assignment for information on
constraints, types, side effects, and conversions and the effects of operations on pointers.
关于非lvalue:
ISO C99:
6.5.3.1
1 Anlvalue is an expression with an object type or an incomplete type other than void;53) if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure
or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.
53) The name "lvalue" comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object "locator value". What is sometimes called "rvalue" is in this International Standard described as the "value of an expression".
rvalue显然不是lvalue。在ISO C中,rvalue和value等同。因此,value不是lvalue。
FrankHB1989 2011-10-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 blue_shy_bruce 的回复:]

引用 2 楼 demon__hunter 的回复:
在c++03中1错2未指定行为
c++11中1错2未定义行为

未定义行为可以理解为在不同的编译环境下可能会得到不同的结果吗?
[/Quote]
ISO C99:
6.5.16 Assignment operators
Constraints
2 An assignment operator shall have a modifiable lvalue as its left operand.
↑根据这条应该认为非左值作为=的做操作数是错误的(如果不是constraints中的shall被违反,根据Clause 4,程序存在未定义行为,而不是被直接认为是错误的程序导致编译失败)。
具有未定义行为的程序是指程序具有错误的/不兼容的构造或数据,不能预期有合理的行为,即在逻辑上应该是错误的,尽管可以在形式上是正确的(well-formed)ISO C++程序——它可以不违反可诊断语义、语法规则和ODR。(在ISO C中未定义行为的范围更广,包括语言实现;ISO C的那些在constraints之外部分的未定义行为和ISO C++的相当。一个符合标准的编译器在遇到这样的未定义行为时,有权不告诉用户存在编译错误而是崩溃之类,尽管这没什么现实意义。)
具有未指定行为的程序可能是正确的,但也没有在相同的环境下就一定有相同行为的保证。事实上,对于未指定行为,即使是同一个程序片段在同一个环境下,实际都可以不同。
另外,C++03中说2这样的程序产生未指定行为,后来被标准委员会认为是错误的,是标准的bug,被后来的defect report修正,也就是现在的C++11规定的结果。
bluenets 2011-10-23
  • 打赏
  • 举报
回复
l value
proorck6 2011-10-23
  • 打赏
  • 举报
回复
如果这段代码出了问题……
lz需要对此类代码负完全刑事责任。
AnYidan 2011-10-23
  • 打赏
  • 举报
回复
(a++) += a; // 不是 l-value

如果lz 坚持写此类的代码,自己就该知道结果
无痕云海 2011-10-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 programmingring 的回复:]
在C中自增操作不能作为左值
[/Quote]
这个可以从某个角度进行理解吗,还是标准规定的?
无痕云海 2011-10-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 demon__hunter 的回复:]
在c++03中1错2未指定行为
c++11中1错2未定义行为
[/Quote]
未定义行为可以理解为在不同的编译环境下可能会得到不同的结果吗?
ProgrammingRing 2011-10-23
  • 打赏
  • 举报
回复
在C中自增操作不能作为左值
机智的呆呆 2011-10-23
  • 打赏
  • 举报
回复
在c++03中1错2未指定行为
c++11中1错2未定义行为
机智的呆呆 2011-10-23
  • 打赏
  • 举报
回复
在c中都是错误的(++a)和(a++)都不是左值

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧