I came across the foll code:

#include<stdio.h>

main()
{
   int i=4,j=7;

   j=j||(printf("you can")&&(i=5));
   printf("%d %d",i,j);
}

output: 4 1
Athough I am specifying the braces for the && operator so that it gets executed first..Then also the value of i remains 4 only..Why doesnot it gets changed to 5??Also the printf doesnot execute??

Athough I am specifying the braces for the && operator so that it gets executed first.

It doesn't work that way. The logical operators are short circuiting, and they always execute left to right. Those two features together mean that the left side of your || statement will always occur first. Because that test evaluates to true, there's no point in executing the right side; the final result is known, and side effects on the right side are immaterial as far as the logical tests are concerned.

It doesn't work that way.

Is it in case of logical operators only or every binary operator??..Because since i Have mentioned the brace for && operator to make it execute first..In case of other binary operators like arithmetic ones you can increase the priority by specifing braces as in case of a=4*(5+6) here + will execute first then why dosent that happen in case of && operator in my expression

Is it in case of logical operators only or every binary operator??

I could have sworn I clearly said "the logical operators are short circuiting". If other operators had that behavior, I would have mentioned it. So yes, it's only in the case of logical operators (&& and ||). Technically the conditional operator (?:) is also short circuiting, but that's so obvious and intrinsic to its behavior that there's little meaning in pointing it out.

Because since i Have mentioned the brace for && operator to make it execute first..In case of other binary operators like arithmetic ones you can increase the priority by specifing braces as in case of a=4*(5+6) here + will execute first then why dosent that happen in case of && operator in my expression

I could have sworn I clearly said "it doesn't work that way", and then went on to explain how it did work with logical operators. If there's a part of my explanation you didn't understand, please point it out and I'll try to elaborate. But please don't completely discard everything I said that answered your question and then ask the same damn question again.

Because since i Have mentioned the brace for && operator to make it execute first

The parentheses () do not make the contained code execute first. That is a simplistic way to think of them and wrong. The parentheses create a sub-expression with-in a complete expression. The sub-expression is evaluated when it's value is required.

So in the statement

j = j || (printf("you can")&&(i=5));

the expression is j || (printf("you can")&&(i=5)) which consists of 2 sub-expressions j and (printf("you can")&&(i=5)). The operator using these 2 sub-expressions is || which as deceptikon explained is a short-circuiting operator (only || and && are short circuit operators in C/C++) that means it evaluates its left hand expression first, since this is or if it is true the answer can be deduced from that single result and the right hand expression is not evaluated. j is 7 and this equates to true so in your code the left hand sub-expression j is evaluated as true and because of that the right hand sub-expression (printf("you can")&&(i=5)) is never evaluated.

In your more simple example a=4*(5+6) the parentheses create a sub-expression (5+6) but since the multiplication operator requires the values of both its sub-expression this is always evaulated. It is platform dependent weather the sub-expressions (5+6) or 4 is actually evaluated first but both are evaluated before the multiplication is done.

Precedence is not about which operator is evaluated first, it is about how the operands bind to the operators.

So in the expression A || (B && C) the parentheses overide precedence, B and C bind to && and A and the result of the && bind to the ||.

In the expression A || B && C there are no parentheses to overide precedence, so in this case precedence takes effect an since && has a higher precedence the B and C bind to the && and A and the result of the && bind to the ||. Note this is the same as the previous example.

In the expression (A || B) && C the parentheses actually overide precedence. Normally the && having a higher precedence would use B as it's left hand side however because of the parentheses the A and B bind to || and the result of the || and C bind to the &&.

Again this is not about which operator gets evaluated first, they are evaluated as they are needed. This is about what each operator uses as its operands.

@Banfa great explaination thanks it cleared my doubts!!!!! one last doubt.To conclude can we say that order of evaluation is left to right or comipler dependent??

Edited 3 Years Ago by saurabh.mehta.33234

Actually th order of evaluation of operators of the same precedence is defined by the language. If you look at the page you linked to 3 posts ago then it is listed there as the operators associativity.

This question has already been answered. Start a new discussion instead.