Question on Operator Precedence

S

Sac

Hello C Gurus,
As per my understanding given a C expression higher
precedence operators are evaluated first.
For example X=Y*3+Z*3;
As * has higher precedence Y*3 and Z*3 gets evaluated 1st
followed by +.
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.

Thanks
Sachin
 
B

Ben Bacarisse

Sac said:
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.
 
F

Falcon Kirtaran

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello C Gurus,
As per my understanding given a C expression higher
precedence operators are evaluated first.
For example X=Y*3+Z*3;
As * has higher precedence Y*3 and Z*3 gets evaluated 1st
followed by +.
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.

Thanks
Sachin

To be extremely brief, when you are using boolean operators, the
code basically only evaluates as much of it as necessary. If ++x is
true and x was not -1, it already knows the whole thing is true (true or
anything is true) and so never looks at ++y && ++z, meaning they don't
get executed.

Also note: if x was -1 to begin with, both ++y and ++z would be evaluated.

- --
- --Falcon Darkstar Christopher Momot
- --
- --OpenPGP: (7902:4457) 9282:A431

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQIcBAEBAgAGBQJJ7pcYAAoJEKmxP9YxEE4rV/QP+wSychtOlK0ALyD6XMCs97Xb
EuG1q6TlpnxT6AIP6bEAwg+UaIdexCemelH8JIi8PBp/+UwvUCi6hVUWM9it3LhB
CwvDCA+Jj4JoM6icnzXVz8WD36EFsa6mBrs4Ef7z4q3B7rGaMriKsBYd36szFflP
tFJMVbtWbMrFYLPOh9H17q5zKgQ2g8dKwKZGv+gBawicZtP87Y7I+E0c/b2kIlID
3CHH+cYxxHZt77lIU4Y79qUchl8RVSKJviiL0BVgvuM+RyRPWv2L9NfnTFnNdXf8
tSxtqAKpB3tZqIB8+b3PAgUqfVrwylzAUNipglXO3ROwnlfkXALfPZvksyP2hU7h
i4Gs6pAy4dJ1/iQyrC/zGNsQkfnoG+66cB4XVzhyzbH5DgJaSx8s0+0LzEvN1Koy
K4xMYeldpP/IttyvlPTdRlUbzZsqxo6ReOisGuOl6Ddh1zACCODMkcgt6nNEtfUt
VTCo8uDKM8juSDR4aTHrGJ1CE3tTomx8fP58duLqnRqgjVQqBT2J3zacu/VquOb7
EhxNlZHaJNRyAmwZuPLSq9Ndzh98QSW9liiyHw/fb8QPmYNgEfLs5/Rt1pviJDX3
3kk/d5//MbhHVqrPIDWt2UrRe2DJT7lE5LlDEiKVwnIr3/sH66Uwo0E1xszs70Oh
S6yyZ9m6ElrSq4eSQrq5
=wDjz
-----END PGP SIGNATURE-----
 
E

Eric Sosman

Falcon said:
Sac said:
[...]
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.

To be extremely brief, when you are using boolean operators, the
code basically only evaluates as much of it as necessary. If ++x is
true and x was not -1, it already knows the whole thing is true (true or
anything is true) and so never looks at ++y && ++z, meaning they don't
get executed.

Also note: if x was -1 to begin with, both ++y and ++z would be evaluated.

Not if y was also originally -1. Then you'd have

++x = 0 (false). Since "false OR whatever" depends
on the value of "whatever," the second sub-expression
also needs evaluation. So, onward:

++y = 0 (false). Since "false AND anything" does *not*
depend on the value of "anything," the second operand
of the && is not evaluated. The result of the whole
expression is thus

false || (false && unevaluated) = false

.... so ++x and ++y are evaluated, but that ++z is not.

Here's the rule for &&: Evaluate the first operand. If
it's false, the entire expression must be false (false AND
anything = false), and the second operand is not evaluated.
If the first was true, the expression's value is that of the
second operand (true AND something = something), so the second
operand is evaluated.

The rule for || is the dual of the && rule: Evaluate the
first operand. If it's true, the entire expression is true
(true OR anything = true), and the second operand is not
evaluated. If the first operand is false, the expression's
value is that of the second operand (false OR something =
something), so the second operand is evaluated.
 
C

Chris Dollin

Sac said:
Hello C Gurus,
As per my understanding given a C expression higher
precedence operators are evaluated first.

No.

Precedence is about grouping -- which expressions are operands
of which operators. Evaluation order is about when things happen.
They're linked because typically an operator can't be evaluated
until the values of its operands are known. (/Typically/, meaning
that there are some operators that it's not true for.)
For example X=Y*3+Z*3;
As * has higher precedence Y*3 and Z*3 gets evaluated 1st
followed by +.

This explanation confuses the grouping and the evaluation order.

The higher precedence of `+` means that the expression above is
shorthand for

X = ((Y * 3) + (Z * 3));

rather than, say,

X = ((Y * (3 + Z)) * 3)

so that the `_+_` must be evaluated after both the `Y*3` and the
`Z*3` -- but /their/ order is not specified.
However when I tried the following example
++x || ++y && ++z;
Y and Z never got incremented.

The expression `++x || ++y && ++z` is shorthand for

++x || (++y && ++z)

The `||` operator has an atypical evaluation rule. The left
operand is evaluated. If it is true (non-zero), we're done;
the who expression is true (1). Otherwise the right operand
is evaluated. In this case, only if `x` starts off as `-1`,
so that the result of `++x` is 0, will `(++y && ++z)` be
evaluated.
As ++ operator is of higher precedence I think all
variables should be incremented first followed by || operator.
No.

Please let me know if there are other rules in C while
evaluating an expression other than precedence and associativity.

There are.
 
C

CBFalconer

Sac said:
As per my understanding given a C expression higher
precedence operators are evaluated first.
For example X=Y*3+Z*3;
As * has higher precedence Y*3 and Z*3 gets evaluated
1st followed by +.
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.

Yes. Try reading K&RII or the C standard. K&R is simpler.

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/> (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2> (pre-C99)
<http://www.dinkumware.com/c99.aspx> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top