operator precedance (&& or ||)

V

vaysagekv

hi,
For the below program i expected output of -3 3 1 1 because precedance
of && is greater than ||. But the actual output is -2 2 0 1. why?

int main ()
{


int i=-3,j=2,k=0,m;
m = ++i || ++j && ++k;
printf("\n%d%d%d%d\n",i,j,k,m);


}
 
N

Nick

vaysagekv said:
hi,
For the below program i expected output of -3 3 1 1 because precedance
of && is greater than ||. But the actual output is -2 2 0 1. why?

int main ()
{


int i=-3,j=2,k=0,m;
m = ++i || ++j && ++k;
printf("\n%d%d%d%d\n",i,j,k,m);


}

Because || is a short-cut operator.

So although && binds more tightly than || (so the middle line is "m =
(++i) || (++j && ++k);" rather than "m = (++i || ++j) && (++k);"), once
the ++i returns a true value, then second half (both sides of the &&)
doesn't need to be executed at all.

The short-cut nature of these two operators - && in particular - is very
useful in C. It lets you write, for example:

if(s && *s != '\0') {

which checks that s is non-null, and that it points at a non-zero-length
string, without the second part causing an error when s is null.
 
W

Willem

vaysagekv wrote:
) hi,
) For the below program i expected output of -3 3 1 1 because precedance
) of && is greater than ||. But the actual output is -2 2 0 1. why?
)
) int main ()
) {
) int i=-3,j=2,k=0,m;
) m = ++i || ++j && ++k;
) printf("\n%d%d%d%d\n",i,j,k,m);
) }

Probably because you misunderstand how precedence works.
Perhaps you should explain in more detail why you expected that output.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
V

vaysagekv

vaysagekv wrote:
) hi,
) For the below program i expected output of -3 3 1 1 because precedance
) of&& is greater than ||. But the actual output is -2 2 0 1. why?
)
) int main ()
) {
) int i=-3,j=2,k=0,m;
) m = ++i || ++j&& ++k;
) printf("\n%d%d%d%d\n",i,j,k,m);
) }

Probably because you misunderstand how precedence works.
Perhaps you should explain in more detail why you expected that output.


SaSW, Willem
I thought first && part will be calculated and then || part (i.e if we
have expression like a+b*c,first b*c will be calculated right)
 
V

vaysagekv

Because || is a short-cut operator.

So although&& binds more tightly than || (so the middle line is "m =
(++i) || (++j&& ++k);" rather than "m = (++i || ++j)&& (++k);"), once
the ++i returns a true value, then second half (both sides of the&&)
doesn't need to be executed at all.

The short-cut nature of these two operators -&& in particular - is very
useful in C. It lets you write, for example:

if(s&& *s != '\0') {

which checks that s is non-null, and that it points at a non-zero-length
string, without the second part causing an error when s is null.
Thanks a lot
 
W

Willem

vaysagekv wrote:
) On 04/07/10 9:11 PM, Willem wrote:
)> vaysagekv wrote:
)> ) hi,
)> ) For the below program i expected output of -3 3 1 1 because precedance
)> ) of&& is greater than ||. But the actual output is -2 2 0 1. why?
)> )
)> ) int main ()
)> ) {
)> ) int i=-3,j=2,k=0,m;
)> ) m = ++i || ++j&& ++k;
)> ) printf("\n%d%d%d%d\n",i,j,k,m);
)> ) }
)>
)> Probably because you misunderstand how precedence works.
)> Perhaps you should explain in more detail why you expected that output.
)>
) I thought first && part will be calculated and then || part (i.e if we
) have expression like a+b*c,first b*c will be calculated right)

Well, yes and no. It's not specified in which order a, b and c are read.

Suppose you do a()+b()*c(), then it's entirely possible that a() will be
called first, and then b() and c().

Also, if you do a()+b() then it's also very possible that b() will be
called first, and then a().


But || and && are special. They evaluate the left side first, and
*only* if that's inconclusive, it will evaluate the right side.

So if you do a() || b(), then you *know* that a() will be called first.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
D

Denis McMahon

For the below program i expected output of -3 3 1 1 because precedance
of && is greater than ||. But the actual output is -2 2 0 1. why?
int main () {
int i=-3,j=2,k=0,m;
m = ++i || ++j && ++k;
printf("\n%d%d%d%d\n",i,j,k,m);
}

The boolean "a || (b && c)" can stop after evaluating a if a is true, as
that renders the whole expression true. So, ++j and ++k are never
evaluated if ++i is true.

Try setting i to -1.

++i evaluates to '0' (or false) so "++j && ++k" needs to be evaluated,
giving 3 && 1 (true && true) so the whole expression becomes (false ||
(true && true)) which evaluates to true (or 1 when expressed as an int),
giving 0311 I think.

Rgds

Denis McMahon
 
E

Eric Sosman

There is no precedence for&& over ||, they are both evaluated left to
right until the result is known.

No, that's wrong. The evaluation order is left to right, as
Geoff says, but && does in fact have higher precedence ("binds more
tightly") than ||.
You need to read K&R2, page 41 in chapter 2.6

Can't: Don't have it. But K&R Classic spells this out in the
table on page 49 of section 2.12. The actual ISO Standard uses a
formal grammar rather than "precedence" and "associativity," but
the result amounts to the same thing: See sections 6.5.13 and 1=6.5.14.
The operands of logical-AND and logical-OR expressions are evaluated
from left to right. If the value of the first operand is sufficient to
determine the result of the operation, the second operand is not
evaluated. This is called short-circuit evaluation. There is a
sequence point after the first operand.

Ah, but what are the operands? When we see an expression like

r || s && t || u

precedence and associativity (or the Standard's formal grammar) tell
us that it means

r || (s && t) || u

rather than

(r || s) && (t || u)

or some other grouping. P&A (grammar) resolve the question of which
sub-expressions are operands of which operators; evaluation order is
a separate issue entirely.
 
I

iC and iC++

hi,
For the below program i expected output of -3 3 1 1 because precedance
of && is greater than ||. But the actual output is -2 2 0 1. why?

int main ()
{

        int i=-3,j=2,k=0,m;
        m = ++i || ++j && ++k;
        printf("\n%d%d%d%d\n",i,j,k,m);



}
This is another question from the C puzzle book, chapt 2.. A thorough
explanation is provided in the solutions section in case you missed it
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top