免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1821 | 回复: 4
打印 上一主题 下一主题

关于i++ 和 ++i [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-12 23:48 |只看该作者 |倒序浏览
说实话,我一直都不是很清楚对于i++ 这种,具体是什么时候才实行自增。

因为我不清楚表达式的严格定义。。。

对于语句:
int i = 1;
int j = i++;

地球人都知道 j 等于 1,但是 i 的自增是什么时候发生的呢? 如果说 i 在表达式求值 之后才自增,也就是说:
先求表达式 i++ 的值,然后自增 i,并把表达式的值赋给 j 。毫无疑问,这可以行得通,但是对于下面的语句:

int i = 1;
int j = (i++) + (i++);// 不要说我写了烂代码-_-,只是用来说明问题。。。。

如果按照上面的思路,先求表达式i++的值,为1,然后递增i,再求另一个表达式i++的值,为2,自增i,再相加两个表达式,最后的结果是j =  
1 + 2 = 3;但GCC实际上会搞成j = 2,它是这样做的,求一个表达式 i++ 的值,为1,再求另一个表达式i++的值,也为1,得出j = 1+1 = 2,最后在自增 i 两次。这里我们可以把 (i++) + (i++) 看成一个大的表达式,这样的话,”i 在表达式求值 之后才自增“ 这个结论又可以成立了,也可以看做“i 在一条语句执行完之后才自增”,同样也是成立的。

如果是
int j = (i++) + (i++) + (i++);

那么究竟是作为3个“i++”表达式来考虑,还是作为“(i++) + (i++) ”和“i++"这两个表达式来考虑,或者作为”(i++) + (i++) + (i++)“这一个大的表达式来考虑,或者直接作为一条语句来考虑。。。。呃,顺便说句,gcc下结果为1+1+1 = 3,是作为最大的那个表达式来考虑的,也可以是作为一条语句来考虑的。

虽然这看上去像是编译器自己的事,但是还是希望有人能把关于i++ 和 ++i 在C标准中的定义贴出来看看,我手头没有。虽然我们都知道它们的用法,而且都用的很好,但就我个人而言还是希望能彻底弄清楚,大概我精力过旺。。。另希望有人能明确告知表达式的严格定义,编译原理课还没开始上。。。

论坛徽章:
0
2 [报告]
发表于 2008-10-12 23:59 |只看该作者
原帖由 kiffa 于 2008-10-12 23:48 发表
说实话,我一直都不是很清楚对于i++ 这种,具体是什么时候才实行自增。

因为我不清楚表达式的严格定义。。。

对于语句:
int i = 1;
int j = i++;

地球人都知道 j 等于 1,但是 i 的自增是什么时候 ...

6.5.2.4  Postfix increment and decrement operators
Constraints
1      The  operand  of  the  postfix  increment  or  decrement  operator  shall  have  qualified  or
unqualified real or pointer type and shall be a modifiable lvalue.
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.
3      The postfix -- operator is analogous to the postfix ++ operator, except that the value of
the operand is decremented (that is, the value 1 of the appropriate type is subtracted from
it).

其他的 标准未定义

论坛徽章:
0
3 [报告]
发表于 2008-10-13 00:09 |只看该作者
首先感谢一下楼上的热心。

The side effect of updating the stored value of the operand shall occur between
the previous and the next sequence point.

看到了序列点,不过我忘记具体定义了,我先去查查看。另外麻烦把++i 的也贴出来吧。

论坛徽章:
0
4 [报告]
发表于 2008-10-13 00:23 |只看该作者
原帖由 kiffa 于 2008-10-13 00:09 发表
首先感谢一下楼上的热心。

The side effect of updating the stored value of the operand shall occur between
the previous and the next sequence point.

看到了序列点,不过我忘记具体定义了,我先去 ...

6.5.3.1  Prefix increment and decrement operators
Constraints
1      The  operand  of  the  prefix  increment  or  decrement  operator  shall  have  qualified  or
unqualified real or pointer type and shall be a modifiable lvalue.
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 ++Eis 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.
3      The prefix --operator is analogous to the prefix ++operator, except that the value of the
operand is decremented.
Forward references:  additive operators (6.5.6), compound assignment (6.5.16.2).

论坛徽章:
0
5 [报告]
发表于 2008-10-13 08:55 |只看该作者
blizzard213 兄弄的是不是C标准啊?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP