简体   繁体   English

以下代码的执行顺序是什么?

[英]What is the execution sequence for the code below?

( s1[i] = s2[i] ) != '\0'

不等式是否用'\\0's1[i]检查s2[i] s1[i]

s2[ i ] will be assigned to s1[ i ] and then value of s1[ i ] will be compared against ZERO. s2[ i ]将被分配给s1[ i ]然后的值s1[ i ]将针对ZERO进行比较。

Please refer here for more info. 参考这里获取更多信息。

In your language, it does unequality check with s1[ i ] , but in the following sequence, 用您的语言,它使用s1[ i ]不相等检查,但按以下顺序,

  1. Assign value of s2[i] to s1[i] s2[i]值分配给s1[i]
  2. Check the value of s1[i] with \\0 \\0检查s1[i]的值
  3. Returns 1 or 0 based on step 2 . 根据步骤2返回10

The expression of the assignment returns the assigned value : 赋值的表达式返回赋值

The value of the expression is the value of the left operand after the assignment has completed. 表达式的值是赋值完成后左操作数的值。

So when you have: 所以当你有:

( s1[ i ] = s2[ i ] ) != '\\0'

Then you're comparing the assigned value (which is s1[i] ) to '\\0' . 然后,您将分配的值 (即s1[i] )与'\\0'

The inequality first makes the assignment ( s1[i] = s2[i] ), then checks that s1[i] is not '\\0' . 不等式首先进行赋值( s1[i] = s2[i] ),然后检查s1[i]是否不是'\\0'

I assume it was in an if so: 我认为是这样的:

if ((s1[i] = s2[i]) != '\0')
    ;// Do things

is equivalent to : 等效于:

s1[i] = s2[i];
if (s1[i] != '\0')
    ;// Do things

Operator precedence and order of evaluation determines the expression's evaluation sequence. 运算符的优先级顺序确定表达式的求值顺序。

Operator precedence states in which order to interpret an expression, ie which operators belong to which operand. 运算符优先级说明了解释表达式的顺序,即哪个运算符属​​于哪个操作数。 It is similar to mathematical precedence, for example, 1+1*2 is 3 not 4, because * has higher precedence than + . 它与数学优先级相似,例如1+1*2为3而不是4,因为*优先级高于+ Because of that precedence rule, the expression gets evaluated as 1+(1*2) . 由于该优先规则,表达式的求值为1+(1*2)

Order of evaluation is something different from precedence. 评估顺序与优先顺序有所不同。 It states the order in which sub-expressions are evaluated. 它说明了子表达式的计算顺序。 Another math example: we know that (1*1) + (1+1) is always 3. But when we calculate that, we could decide to start with the left parenthesis first, or we could start with the right one. 另一个数学示例:我们知道(1*1) + (1+1)始终为3。但是,在计算时,我们可以决定首先从左括号开始,也可以从右括号开始。 The precedence of the operators does not decide which one we start with. 运算符的优先级并不能决定我们从哪一个开始。 Even though * has higher precedence than + , we could start calculating the right side expression first, and it wouldn't affect the result. 即使*优先级高于+ ,我们也可以首先开始计算右侧表达式,并且不会影响结果。

But when programming, this order of evaluation often matters. 但是在编程时,这种评估顺序通常很重要。 The compiler is the one deciding this order. 编译器是确定此顺序的人。 And to make things complicated, it is allowed do as it pleases on case-to-case basis, without documenting how (" unspecified behavior "). 为了使事情变得复杂,允许根据情况按自己的喜好进行操作,而无需记录操作方式(“ 未指定的行为 ”)。


  • The () operator has the highest precedence, so ( s1[i] = s2[i] ) will get evaluated first. ()运算符具有最高优先级,因此( s1[i] = s2[i] )将首先被求值。 Everything inside it is a sub-expression. 其中的所有内容都是一个子表达式。
  • Inside the () sub-expression, the [] operator has the next precedence, so the two " s[i] " are evaluated next. 在()子表达式内部,[]运算符具有下一个优先级,因此接下来将对两个“ s[i] ”进行求值。 Please note that the " s[i] " are also sub-expressions, and because the order of evaluation of sub-expressions isn't specified, the compiler is free to evaluate s1[i] before or after s2[i] . 请注意,“ s[i] ”也是子表达式,并且由于未指定子表达式的求值顺序,因此编译器可以在s2[i]之前或之后自由评估s1[i] s2[i]

    In this specific case, which one that gets evaluated first most likely doesn't matter. 在这种特定情况下,最先获得评估的是最重要的。

  • Inside the () sub-expression, the = operator comes next. 在()子表达式中,=运算符紧随其后。 The contents of the operand s2[i] is copied into s1[i] . 将操作数s2[i]内容复制到s1[i]

  • All expressions inside the () are now evaluated, we have a result, stored in s1[i] . 现在对()中的所有表达式求值,我们得到一个结果,存储在s1[i]
  • And finally, this result from the sub-expression is one operand of the remaining operator of the expression: != . 最后,子表达式的结果是表达式其余运算符的一个操作数: != The other operand is the character literal '\\0' . 另一个操作数是字符文字'\\0'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM