C is the order of estimation for the equation

I have done a lot of research on how the evaluation of the evaluation goes, but I can’t understand how it will look for this equation:

z = !x + y * z / 4 % 2 - 1 

My best guess (left to right):

 z = !x + {[([y * z] / 4) % 2] - 1} 
+4
source share
2 answers

Evaluation order and operator priority are two different things.

Your best guess is true. All multiplicative * / % operators have the same priority and are linked from left to right. Additive operator - has a lower priority. Unary operator ! binds more strongly than multiplicative or additive operators. And the assignment operator = has a very low priority (but even higher than the comma operator).

So this is:

 z = !x + y * z / 4 % 2 - 1 

equivalent to this:

 z = (!x) + (((y * z) / 4) % 2) - 1 

But the operands can be evaluated legally in any order (with the exception of some operators, such as && , || which impose an evaluation from left to right). If the operands are simple variables, this probably doesn't matter, but something like:

 z = func(x) * func(y); 

two function calls can be executed in any order.

+7
source

If you cannot understand this, rewrite your expression

 z = !x + y * z / 4 % 2 - 1 notx = !x; /* you can move this line 1, 2, or 3 lines down */ tmp1 = y * z; tmp2 = tmp1 / 4; tmp3 = tmp2 % 2; tmp4 = notx + tmp3; tmp5 = tmp4 - 1; 
+1
source

All Articles