运算符的结合律?

cornz 2012-10-11 10:18:48
置顶贴:

m = 1;
n = m+++m++;

运算符:++,是有副作用的,它是从右到左结合。如果,有多个+,按照结合律,应该怎样结合?
上面的代码,是怎样结合?
...全文
377 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gloveing 2012-10-11
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
引用 7 楼 的回复:

不要写连自己也预测不了结果的代码!
下面内容仅供参考:
C/C++ code
// C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+--------------……
[/Quote]
http://www.cppblog.com/aqazero/archive/2006/06/08/8284.html
Alexander 2012-10-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

不要写连自己也预测不了结果的代码!
下面内容仅供参考:
C/C++ code
// C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+------------------------------……
[/Quote]
想问下赵老师这个优先级表哪儿查到的?

话说其中有同级的运算符吧,比如*、/和%。
buyong 2012-10-11
  • 打赏
  • 举报
回复
这是个没有答案的问题。不同编译器结果可能不一样的,官方也禁止这么写
gaoyang2 2012-10-11
  • 打赏
  • 举报
回复
优先级这个问题,看了下++ > + 的优先级 结合性默认是右结合,m+++m++ <=> (m++) + (m++))
(++(m++))要报错
+ 左向右结合
++ 右向左结合
lin5161678 2012-10-11
  • 打赏
  • 举报
回复
据说 置顶有说明了
学会使用搜索工具
关键字
C语言 未定义行为[以下"C语言"略]
顺序点
序列点
副作用
副效应
sequence point
...........
赵4老师 2012-10-11
  • 打赏
  • 举报
回复
不要写连自己也预测不了结果的代码!
下面内容仅供参考:
// 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 |
//+------------------+-----------------------------------------+---------------+
赵4老师 2012-10-11
  • 打赏
  • 举报
回复
VC调试时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。
(Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)

不要迷信书、考题、老师、回帖;
要迷信CPU、编译器、调试器、运行结果。
并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。
任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!
zhcosin 2012-10-11
  • 打赏
  • 举报
回复
纠结这个有意思吗,计算机怎么处理这个要看编译器是怎么解释的,编译器说是 (m++)+n 就是 (m++)+n,编译器说是 m+(++n) 就是 m+(++n),有时间纠结这个,不如自己实现一个小型编译器,自然明白其中的道理。
cornz 2012-10-11
  • 打赏
  • 举报
回复
运算符:++,从右向左结合。是结合运算符++左边的操作数的意思? 如: ++a++; ++(a++); 对否?
binxinxuelian 2012-10-11
  • 打赏
  • 举报
回复
自增自减运算符只能用于变量,而不能用于常量或表达式。在vc++6.0中,n=(m++)+(m++)=2.
这个不同的编译器会有不同结果。最好在写这样的程序时加括号,以便更清楚。
wymags2012 2012-10-11
  • 打赏
  • 举报
回复
这是个问题
cornz 2012-10-11
  • 打赏
  • 举报
回复
各位:
3楼问题?
赵4老师 2012-10-11
  • 打赏
  • 举报
回复
自己写代码搞不清楚算符优先级请多加括号。

看别人代码搞不清楚算符优先级能调试的话请单步调试对应汇编:
VC调试时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。
(Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)

看别人代码搞不清楚算符优先级不能调试的话想办法照写一小段孤立的可调试的代码然后单步调试对应汇编。

看别人代码搞不清楚算符优先级不能调试的话且没有办法照写一小段孤立的可调试的代码然后单步调试对应汇编的话只能参考我上面回帖中给出的算符优先级表猜了(提醒:并不能100%猜对)。
lin5161678 2012-10-11
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
[/Quote]
企图用优先级结合性 或者是某一种汇编解释 i=i++
只会死得很惨
Alexander 2012-10-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

引用 11 楼 的回复:
引用 7 楼 的回复:

不要写连自己也预测不了结果的代码!
下面内容仅供参考:
C/C++ code
// C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+-----------------……
[/Quote]
谢谢了

69,373

社区成员

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

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