C语言的优先级和结合律?

cozim 2012-10-07 06:05:37
x+++y;
这个表达式的优先级,结合律是什么?
...全文
413 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhcosin 2012-10-09
  • 打赏
  • 举报
回复
纠结这干啥,语法都是编译器定的,如果你有足够的水平,自己写两个解释器按照两种思路去解释它,你说哪个正确?
有空纠结这个,还不如自己实现个小型编译器,你就知道其实这些都是编译器定的。
bravery36 2012-10-09
  • 打赏
  • 举报
回复
蛋疼日经贴,明明都置顶了还有。
_暮落_ 2012-10-09
  • 打赏
  • 举报
回复
何必记规律 用到的时候直接加括号 清晰明了 不容易出错 还好用
在调试的时候就显示出来你的优势了
lin5161678 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 的回复:]
没有任何学的必要,这种垃圾代码我完全没有兴趣。钻究这种无意义小道为的是什么?
[/Quote]

不是学写这种代码 我去 这代码我也鄙视
而是学习分辨什么的未定义行为 什么不是
然后你才能避免无意中写出 包含未定义行为的代码
赵4老师 2012-10-09
  • 打赏
  • 举报
回复
// C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator | Name or Meaning | Associativity |
//+------------------+-----------------------------------------+---------------+
//| :: | Scope resolution | None |
//| :: | Global | None |
//| [ ] | Array subscript | Left to right |
//| ( ) | Function call | Left to right |
//| ( ) | Conversion | None |
//| . | Member selection (object) | Left to right |
//| -> | Member selection (pointer) | Left to right |
//| ++ | Postfix increment | None |
//| -- | Postfix decrement | None |
//| new | Allocate object | None |
//| delete | Deallocate object | None |
//| delete[ ] | Deallocate object | None |
//| ++ | Prefix increment | None |
//| -- | Prefix decrement | None |
//| * | Dereference | None |
//| & | Address-of | None |
//| + | Unary plus | None |
//| - | Arithmetic negation (unary) | None |
//| ! | Logical NOT | None |
//| ~ | Bitwise complement | None |
//| sizeof | Size of object | None |
//| sizeof ( ) | Size of type | None |
//| typeid( ) | type name | None |
//| (type) | Type cast (conversion) | Right to left |
//| const_cast | Type cast (conversion) | None |
//| dynamic_cast | Type cast (conversion) | None |
//| reinterpret_cast | Type cast (conversion) | None |
//| static_cast | Type cast (conversion) | None |
//| .* | Apply pointer to class member (objects) | Left to right |
//| ->* | Dereference pointer to class member | Left to right |
//| * | Multiplication | Left to right |
//| / | Division | Left to right |
//| % | Remainder (modulus) | Left to right |
//| + | Addition | Left to right |
//| - | Subtraction | Left to right |
//| << | Left shift | Left to right |
//| >> | Right shift | Left to right |
//| < | Less than | Left to right |
//| > | Greater than | Left to right |
//| <= | Less than or equal to | Left to right |
//| >= | Greater than or equal to | Left to right |
//| == | Equality | Left to right |
//| != | Inequality | Left to right |
//| & | Bitwise AND | Left to right |
//| ^ | Bitwise exclusive OR | Left to right |
//| | | Bitwise OR | Left to right |
//| && | Logical AND | Left to right |
//| || | Logical OR | Left to right |
//| e1?e2:e3 | Conditional | Right to left |
//| = | Assignment | Right to left |
//| *= | Multiplication assignment | Right to left |
//| /= | Division assignment | Right to left |
//| %= | Modulus assignment | Right to left |
//| += | Addition assignment | Right to left |
//| -= | Subtraction assignment | Right to left |
//| <<= | Left-shift assignment | Right to left |
//| >>= | Right-shift assignment | Right to left |
//| &= | Bitwise AND assignment | Right to left |
//| |= | Bitwise inclusive OR assignment | Right to left |
//| ^= | Bitwise exclusive OR assignment | Right to left |
//| , | Comma | Left to right |
//+------------------+-----------------------------------------+---------------+
bravery36 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 的回复:]

引用 17 楼 的回复:

有歧义啊。可能(x++)+y,可能x+(++y)

没歧义 x+++y不可能被解释为x+(++y)
你没学好
[/Quote]
没有任何学的必要,这种垃圾代码我完全没有兴趣。钻究这种无意义小道为的是什么?
lin5161678 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
int x=10;
int y=20;

cout<<x<<endl;
cout<<y<<endl;
cout<<(x+++y)<<endl;

VC编译结果是11 20 30
[/Quote]

纯粹主观臆测的吧 你运行过了吗?
x的输出怎么可能是 11
lin5161678 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]
int x=10;
int y=20;

cout<<(x+++y)<<endl;
cout<<x<<endl;
cout<<y<<endl;

VC编译结果是30 11 20
[/Quote]

放心好了 正常的运行结果都应该是 30 11 20
lin5161678 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 的回复:]

有歧义啊。可能(x++)+y,可能x+(++y)
[/Quote]
没歧义 x+++y不可能被解释为x+(++y)
你没学好
lin51616780 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 的回复:]

蛋疼日经贴,明明都置顶了还有。
[/Quote]
置顶的介绍和LZ的问题没关系
lin51616780 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 的回复:]

纠结这干啥,语法都是编译器定的,如果你有足够的水平,自己写两个解释器按照两种思路去解释它,你说哪个正确?
有空纠结这个,还不如自己实现个小型编译器,你就知道其实这些都是编译器定的。
[/Quote]
语法是编译器定的 这句话亮了
lin51616780 2012-10-09
  • 打赏
  • 举报
回复
.......
是不是表达式里面出现 ++就是未定义行为了啊
汗了
没弄懂什么的未定义行为然后 千篇一律的这样说
#1楼的链接的楼主的一篇苦心就这样被糟蹋了
我了了个去
x+++y 不是未定义行为 有正确唯一的解释 不管什么编译器给出的结果都是一直的 就是 (x++)+y
没有疑问 没有错误
douzi073 2012-10-08
  • 打赏
  • 举报
回复
有歧义啊。可能(x++)+y,可能x+(++y)
gou12341234 2012-10-08
  • 打赏
  • 举报
回复
总有公司面试出这种问题,这种公司真是无聊!
ping_IP 2012-10-07
  • 打赏
  • 举报
回复
干嘛较真这问题呢?如果你不确定的话,直接用()括起来。
lzh8430000 2012-10-07
  • 打赏
  • 举报
回复
int x=10;
int y=20;

cout<<(x+++y)<<endl;
cout<<x<<endl;
cout<<y<<endl;

VC编译结果是30 11 20


lzh8430000 2012-10-07
  • 打赏
  • 举报
回复
int x=10;
int y=20;

cout<<x<<endl;
cout<<y<<endl;
cout<<(x+++y)<<endl;

VC编译结果是11 20 30
lzh8430000 2012-10-07
  • 打赏
  • 举报
回复
应该是先x+y 在x自加
左眼看到鬼 2012-10-07
  • 打赏
  • 举报
回复
贪心法,x+++y的话,是x++ 再+上y
zhaoming262350 2012-10-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

1.(x++)+y;
2.x+(++y);
哪个对?
[/Quote] 第一个是真确的
加载更多回复(9)

69,373

社区成员

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

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