Re: Question on Operator Precedence
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Wed, 22 Apr 2009 03:11:24 +0100
Sac <sacindia2000@xxxxxxxxx> writes:
As per my understanding given a C expression higher
precedence operators are evaluated first.
Ah, no. Precedence determines how an expression is parsed.
For example X=Y*3+Z*3;
As * has higher precedence Y*3 and Z*3 gets evaluated 1st
followed by +.
All the precedence rules say is that this means X = (Y*3) + (Z*3); The
compiler can generate code to evaluate this in any order it likes.
Yes, there is (in this case) an obvious one but the compiler need not
do this. In fact, if it knows the value of Y and Z there may be no
multiplication or addition at all. If the compiler can tell that the
result is the same, it is also permitted to add Y and Z and then
multiply by 3.
However when I tried the following example
++x || ++y && ++z;
Y and Z never got incremented.
As ++ operator is of higher precedence I think all
variables should be incremented first followed by || operator.
Please let me know if there are other rules in C while
evaluating an expression other than precedence and associativity.
Precedence and associativity tell us only that this means:
(++x) || ((++y) && (++z))
The operators || and && are special -- they are two of a very small
set of operators that define the order in which things happen. In
this case, first the left operand is evaluated and only if the result
compares equal to zero is the right and side evaluated.
--
Ben.
.
- References:
- Question on Operator Precedence
- From: Sac
- Question on Operator Precedence
- Prev by Date: Re: Float comparison
- Next by Date: Re: Float comparison
- Previous by thread: Question on Operator Precedence
- Next by thread: Re: Question on Operator Precedence
- Index(es):
Relevant Pages
|