int i=5,j=10;
printf("%d\n",(i=15)||(j=26));
printf("%d %d\n",i,j);

THe output I expected was :
1
15 26
The actual output was:
1
15 10

In (i=15)||(j=26),
shouldn't the brackets be done first?Isn't that the precedence order?
So,first i must take 15 then j must take 26 then the || must be done.

The explanation I got was,that when 10 is assigned to i,immediately the || is noticed and since
one of the conditions is true the other one isn't taken care of.
But what happened to operator precedence?

Recommended Answers

All 9 Replies

I do not understand your 2nd line of the code.
The out put comes from the 3rd line.
If you want to print
1
15 26
you have to use and operator.
In your program where you have use or operator, when the compiler gets one value it does not go to the another integer because of or operator as I found.

But what happened to operator precedence?

Your parentheses aren't changing anything, the precedence of (i=15)||(j=26) is identical to i=15||j=26 . Short circuiting rules here, so if i=15 evaluates to true, the latter half of the expression is skipped.

The logical OR operator is a short-circuiting operator; when a true result is found in the first clause of the operation, it does not process the second clause. This applies to the clauses as a whole. Operator precedence doesn't enter into it, in this instance.

When it tries to analyse (i=15)||(j=29),it must use some operator precedence.
Parentheses are at the top of the list.
So mustn't it only work on the things inside brackets(in left to right order),first?
How can 'see' an || and decide to short circuit before all the brackets are dealt with?

When it tries to analyse (i=15)||(j=29),it must use some operator precedence.

Yes. || is the top of that expression, with left to right short circuit behavior. Consider (i = 15) + (j = 29) , + is the top of the expression, but since there's no short circuit behavior, both parenthesized expressions are evaluated prior to +.

With (i = 15) || (j = 29) , the precedence is the same, but short circuit behavior requires that the left expression be fully evaluated and tested to be false before the right expression can be evaluated.

Yes. || is the top of that expression

You mean highest in order of precedence?
No it isn't.Brackets come before.
Isn't that correct?

Shouldn't we simply follow the precedence order blindly?(i.e. compute the higher ones before the lower ones)

So you've seen with your own eyes what happens in reality, but you're arguing that it shouldn't have happened?

You mean highest in order of precedence?

If I meant that, I would have said that. My words were carefully chosen. Think in terms of an expression tree and you'll understand what I meant:

(i = 15) || (j = 29)


         ||

   =           =

 i   15      j   29

Shouldn't we simply follow the precedence order blindly?

No, because precedence isn't the only thing involved. You also have associativity and order of evaluation. In the above example, the order of evaluation is left to right with short circuiting. Parentheses around a sub-expression don't alter the behavior of the parent operator, but they can certainly alter what is viewed as a sub-expression. Consider this:

i = 15 || (j = 29)

Removing parens on the left expression changes things drastically:

i = 15 || (j = 29)


  =

i      ||

    15       =

           j   29

That's the difference in precedence here, not the evaluation order of ||'s sub-expressions. Blindly evaluating everything in parens first and unconditionally is a start, but it's not a perfect guideline. If you don't want to go to the trouble of learning how complex expressions are parsed, just break them down and remove all ambiguity:

int i = 5, j = 10;
int cmp;

i = 15;
j = 26;
cmp = i || j;

printf("%d\n", cmp);
printf("%d %d\n", i, j);

This produces the output you originally expected.

Thank You Narue.
Moschops,
yes I have seen,but wanted to know a more general rule and why a rule I had learnt didn't apply..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.