C语言

soundbean 2021-03-24 07:38:35
请问大佬们 为什么不先算i++啊 ++与!运算级相等,不是从右向左结合吗?
...全文
110 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
源代码大师 2021-05-03
  • 打赏
  • 举报
回复
C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html
赵4老师 2021-03-25
  • 打赏
  • 举报
回复
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   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 |
//+------------------+-----------------------------------------+---------------+
soundbean 2021-03-24
  • 打赏
  • 举报
回复
引用 8 楼 forever74的回复:
就你的问题(因为题目没写分号,我也不写) j=!c&&i++ 来说,它是 j=( (!c) && (i++) ) 的意思,而不是 (j=!c) && (i++) 或者 j=(!c && i)++ 或者别的 这是运算符优先级的作用----注意,我们一直没有开始算呢,讨论的是怎么正确理解那个式子 真的要算结果了,其实优先级分析就退休了。上面第四行告诉我们, 它是个赋值语句,先求右侧表达式的值,然后送给j ----注意,我们首先关注的是这里面优先级最低的赋值运算符啊 上一行里的“先”“然后”描述的是执行次序,但这是由赋值运算符的语义决定的,和优先级完全没有关系 然后我们才注意到要被求值的是个逻辑与运算,这是我们马上反应,左边若为假,右边被忽略----这是逻辑与的语义决定的 至此兴尽,return to sleep;
printf("thanks +++++");
forever74 2021-03-24
  • 打赏
  • 举报
回复
就你的问题(因为题目没写分号,我也不写) j=!c&&i++ 来说,它是 j=( (!c) && (i++) ) 的意思,而不是 (j=!c) && (i++) 或者 j=(!c && i)++ 或者别的 这是运算符优先级的作用----注意,我们一直没有开始算呢,讨论的是怎么正确理解那个式子 真的要算结果了,其实优先级分析就退休了。上面第四行告诉我们, 它是个赋值语句,先求右侧表达式的值,然后送给j ----注意,我们首先关注的是这里面优先级最低的赋值运算符啊 上一行里的“先”“然后”描述的是执行次序,但这是由赋值运算符的语义决定的,和优先级完全没有关系 然后我们才注意到要被求值的是个逻辑与运算,这是我们马上反应,左边若为假,右边被忽略----这是逻辑与的语义决定的 至此兴尽,return to sleep;
soundbean 2021-03-24
  • 打赏
  • 举报
回复
引用 6 楼 forever74的回复:
优先级跟运算次序没有决定性关系,要从小学数学的思维习惯摆脱出来。 运算符优先级其实是个小学语文的概念。 应该听过 下雨天留客天留我不留 这个梗吧? 在恰当的位置加逗号是语文风格 把应该结合在一起的东西括起来是C风格 不用括起来也优先黏糊在一起,这就叫优先级高 这就是为什么int *a[3]是个数组而int (*a)[3]是个指针,优先级是干这个用的。
谢谢您!我明白了 十分感谢
forever74 2021-03-24
  • 打赏
  • 举报
回复
优先级跟运算次序没有决定性关系,要从小学数学的思维习惯摆脱出来。 运算符优先级其实是个小学语文的概念。 应该听过 下雨天留客天留我不留 这个梗吧? 在恰当的位置加逗号是语文风格 把应该结合在一起的东西括起来是C风格 不用括起来也优先黏糊在一起,这就叫优先级高 这就是为什么int *a[3]是个数组而int (*a)[3]是个指针,优先级是干这个用的。
forever74 2021-03-24
  • 打赏
  • 举报
回复
运算符的优先级那是讨论结合性用的,用在编译期间。 而运算顺序,只有 && || ?: , 这四个运算符存在确定的顺序,并且前两个有可能,第三个一定会忽略一部分子表达式的求值。这是运行期概念。
soundbean 2021-03-24
  • 打赏
  • 举报
回复
引用 2 楼 forever74的回复:
哦,至于这个题目,那是短路效应造成的。
写完这题,我突然就不懂运算的优先级了
soundbean 2021-03-24
  • 打赏
  • 举报
回复
引用 2 楼 forever74的回复:
哦,至于这个题目,那是短路效应造成的。
请问有&&运算符的话就直接从左到右计算吗?
forever74 2021-03-24
  • 打赏
  • 举报
回复
哦,至于这个题目,那是短路效应造成的。
forever74 2021-03-24
  • 打赏
  • 举报
回复
莫非你忘了 ++写在i后面了?

70,021

社区成员

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

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