Chinaunix首页 | 论坛 | 博客
  • 博客访问: 37998
  • 博文数量: 9
  • 博客积分: 453
  • 博客等级: 下士
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-18 13:13
文章分类
文章存档

2011年(1)

2010年(3)

2009年(5)

最近访客

分类: C/C++

2009-12-16 15:12:55

今早上见到有人问:
#include
int main(void)
{
    int q,p,j = 5,i = 5;
    q = (i++)+(i++)+(i++);
    p = (++j)+(++j)+(++j);
    printf("q = %d , p = %d , i = %d ,j = %d \n",q,p,i,j);
    return (0);
}
 
在不同系统上,什么运行结果不一样啊?这类问题见多了,实在不想深究,只知道这类表达式太依赖编译器,不宜在编程实践中使用类似表达式。但看到他们在不同系统中编译运行给出的几个结果,也禁不住想着为什么会这样,最后逆向推导,觉得应该是构造语法树的细节差别导致不同结果,当然我也不想在深入研究编译器什么实现的问题了,但想到标准的C/C++是什么定义自增和自减运算的,我应该查明。
 
最后找到了C99标准文档(ISO/IEC 9899:1999(E))的描述如下:
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).
Forward references: additive operators (6.5.6), compound assignment (6.5.16.2).
 
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 ++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.
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).
 
C++2003标准文档(ISO/IEC 14882:2003(E))的描述如下:
 
5.2.6 Increment and decrement [expr.post.incr]
1 The value obtained by applying a postfix ++ is the value that the operand had before applying the operator.
[Note: the value obtained is a copy of the original value ] The operand shall be a modifiable lvalue. The
type of the operand shall be an arithmetic type or a pointer to a complete object type. After the result is
noted, the value of the object is modified by adding 1 to it, unless the object is of type bool, in which case
it is set to true. [Note: this use is deprecated, see annex D. ] The result is an rvalue. The type of the
result is the cv-unqualified version of the type of the operand. See also 5.7 and 5.17.
2 The operand of postfix -- is decremented analogously to the postfix ++ operator, except that the operand
shall not be of type bool. [Note: For prefix increment and decrement, see 5.3.2. ]
 
5.3.2 Increment and decrement [expr.pre.incr]
1 The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated).
The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer
to a completely-defined object type. The value is the new value of the operand; it is an lvalue. If x is not
of type bool, the expression ++x is equivalent to x+=1. [Note: see the discussions of addition (5.7) and
assignment operators (5.17) for information on conversions. ]
2 The operand of prefix -- is modified by subtracting 1. The operand shall not be of type bool. The
requirements on the operand of prefix -- and the properties of its result are otherwise the same as those of
prefix ++. [Note: For postfix increment and decrement, see 5.2.6. ]
 
 
继续查看additive operators 和Assignment operators 的相关信息,也没看到标准文档里有涉及到表达式因子的运算顺序(不是运算符号的运算顺序!),所以我下定结论:不同编译器在扫描构造语法树时的思路不同,运算结果也应该不同。这问题提醒了我应该尽快仔细读完编译原理,还有要不时查看标准文档的定义。嗯,细节也不是问题。
阅读(1498) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~