Why does ++x || ++y && ++z
calculate ++x
first, even though the precedence of operator &&
is higher than ||
?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
No no no
YesEdit after peer complaints :)
YES!
Top two answers explain it rather well... I'd point out that || is "OrElse".
Right side doesn't get touched if left side is true. Left side of && (AndAlso) doesn't get touched if right side is false.
If you want both sides to get incremented, use | and &.
http://msdn.microsoft.com/en-us/library/2h9cz2eb(v=VS.80).aspx
MSDN has tables that show how parts are "(not evaluated)".
Why touch part 2, if Part 1 pre-determines the outcome deterministically?
Huh?
If you're saying that
&&
binds tighter than||
(which is true), the expression is then equivalent toSince
||
short-circuits, it needs to evaluate the left-hand side first.If you mean that it ought to be equivalent to
The same is still true, since
&&
also short-circuits, meaning the||
needs to be evaluated first, which in turn makes++x
the first thing to evaluate.Unwind, R and others have explained what really happens. So let me just add:
The premise of your question is faulty. The fact that the
&&
has higher precedence doesn't mean that operands that surround it must be evaluated before any operands in the expression with lower precedence. Even where the special case short-circuiting of||
and&&
this wouldn't necessarily be so.For example, consider
a=b+c+d*e
;*
has higher precedence than+
, but that doesn't mean thatd*e
must be evaluated beforeb+c
. It just means that it must be evaluated before we add the product to the expression as a whole. A compiler could evaluate this expression astemp1=d*e
,temp2=b+c
,a=temp1+temp2
or it could evaluatetemp1=b+c
,temp2=d*e
,a=temp1+temp2
. Both would be equally valid.With the short-circuiting behavior of
||
and&&
, there are some additional restrictions placed on order of evaluation.As a side note: In general I would avoid writing code like this. I can easily see another programmer trying to read this code getting confused about just when the increments will happen and when they won't. Well, maybe if you used real variable names it would not look so bizarre.
I do occasionally rely on short-circuiting preventing side effects. Like
I'm relying on the short-circuit to prevent reading if we're already at end of file, Or
I'm relying on the first test to short-circuit on false so I don't do the delete when I shouldn't. But these examples seem pretty straightforward to me, I'd hope any competent programmer would see what I'm doing. But when the examples get cryptic, I think it needs to be broken out. Consider
If
lastDepositAmount()
had a side effect, then ifcustomerType
is retail this side effect will never happen. I don't think that would necessarily be obvious to a reader. (Partly because the function name implies that it is retrieving data and not performing any update, and partly because there is no obvious relationship between the customer type and the amount of a deposit -- these sound like two independent things.) Admittedly, this is subjective. But when in doubt, choose simplicity and clarity over a trivial performance improvement. Always choose simplicity and clarity over "hey this is a way cool use of an obscure feature, anyone reading this will be impressed at how smart I must be to understand the language well enough to do this".Precedence and order of evaluation are not the same thing, especially in this case. All precedence tells you is that the expression should be parsed as
IOW, it means that the expressions being OR'd together are
++x
and(++y && ++z)
. It does not mean that(++y && ++z)
will be evaluated before++x
.For the logical operators
||
and&&
, evaluation is always left-to-right (Online C Standard, Sections 6.5.13 and 6.5.14). For any expressiona
will be completely evaluated;b
will not be evaluated unless the result ofa
is 0 (think ofa
standing in for++x
andb
standing in for++y && ++z
). Similarly, for any expressiona
will be completely evaluated;b
will not be evaluated unless the result ofa
is not 0.So for your case, the expression
++x
is evaluated first. If the result is 0, only then will++y && ++z
be evaluated.||
and&&
are special in that the order of evaluation is guaranteed to be left-to-right. That is not true of the bitwise or arithmetic operators. For example, in the expressionthe expressions
++x
,++y
, and++z
may be evaluated in any order; all precedence tells you is that the result of(y+1) * (z+1)
will be added to the result ofx+1
.