expression evaluation : help!

M

manan.kathuria

hi all ,
the expression in question is

++i&&++j||++k

most sources say that since the result of the || operation is decided
by the LHS itself , the right side is not computed

my point of thinking is that since the unary operator has higher
precedence than || , it will be evaluated before || in any
case...........where am im going wrong

thanks in anticipation

Manan
 
J

Joona I Palaste

(e-mail address removed) scribbled the following:
hi all ,
the expression in question is

most sources say that since the result of the || operation is decided
by the LHS itself , the right side is not computed
my point of thinking is that since the unary operator has higher
precedence than || , it will be evaluated before || in any
case...........where am im going wrong

Where you're going wrong is thinking precedence and order of evaluation
are the same thing. They're nothing of the sort. Precedence dictates how
a string of C tokens is compiled into an expression. Order of evaluation
dictates how that expression is evaluated at run-time.
 
M

Michael Mair

hi all ,
the expression in question is

++i&&++j||++k

most sources say that since the result of the || operation is decided
by the LHS itself , the right side is not computed

my point of thinking is that since the unary operator has higher
precedence than || , it will be evaluated before || in any
case...........where am im going wrong

See FAQ 3.5 and 3.8, best read the whole section 3:
http://www.eskimo.com/~scs/C-faq/s3.html

Note: The ASCII text version posted here by Steve Summit is
more complete/up to date than the above HTML version but can
be obtained following the links from
http://www.eskimo.com/~scs/C-faq/top.html


Cheers
Michael
 
C

Clark S. Cox III

hi all ,
the expression in question is

++i&&++j||++k

most sources say that since the result of the || operation is decided
by the LHS itself , the right side is not computed

This is true. If the left-hand side of || is true, then the right-hand
side will never be evaluated. So, in your above expression, if (++i)
evaluates to a non-zero value, and (++j) does as well, then k will
never be incremented.
my point of thinking is that since the unary operator has higher
precedence than || , it will be evaluated before || in any
case...........where am im going wrong

The point that you're missing is that precedence has nothing to do with
order of evaluation.
 
M

Michael Mair

Joona said:
ITYM also see FAQ 3.4, which answers the OP's question pretty much
directly.

No, I plainly oversaw it when I was quickly scanning for special
questions to point out -- some people are scared by
"read the whole section" ;-)


Cheers
Michael
 
J

Joona I Palaste

No, I plainly oversaw it when I was quickly scanning for special
questions to point out -- some people are scared by
"read the whole section" ;-)

Does your reply "no" to my "ITYM" mean "no, that's just plain wrong" or
"no, I didn't think of that"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top