Lyrk
Lyrk

Reputation: 2000

What is the execution sequence for the code below?

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

Does the inequality check s2[i] with '\0' or s1[i]?

Upvotes: 1

Views: 237

Answers (5)

Lundin
Lundin

Reputation: 213711

Operator precedence and order of evaluation determines the expression's evaluation sequence.

Operator precedence states in which order to interpret an expression, i.e 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 +. Because of that precedence rule, the expression gets evaluated as 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. 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. 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. 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].

    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].

  • All expressions inside the () are now evaluated, we have a result, stored in 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'.

Upvotes: 1

Dayal rai
Dayal rai

Reputation: 6606

s2[ i ] will be assigned to s1[ i ] and then value of s1[ i ] will be compared against ZERO.

Please refer here for more info.

Upvotes: 10

Maroun
Maroun

Reputation: 95958

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'.

Upvotes: 4

Jerska
Jerska

Reputation: 12002

The inequality first makes the assignment (s1[i] = s2[i]), then checks that s1[i] is not '\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

Upvotes: 3

VoidPointer
VoidPointer

Reputation: 3097

In your language, it does unequality check with s1[ i ], but in the following sequence,

  1. Assign value of s2[i] to s1[i]
  2. Check the value of s1[i] with \0
  3. Returns 1 or 0 based on step 2.

Upvotes: 4

Related Questions