in Programming in C edited by
832 views
2 votes
2 votes
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    printf("Hello World");
    int a = 10, b = 20,  c= 30, d = 40;
    int e = ++a || ++b || ++c && ++d;        // DoubtFull Line
    cout<<"a = "<<a<<" b = "<<b<<" d = "<<d<<" c = "<<c<<" e = "<<e; 
    return 0;
}

Consider the code and consider the precedence table given in the link:-

http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

As per the link mentioned in the above, && operator has higher precedence than $||$, and using the short circuit, first

++c && ++d 

will execute which and return true.

Now the expression should be "++a || ++b || TRUE" in this using the "short circuit principle "only ++a will execute but ++b will not so the final Output should be:-

a = 11 b = 20 c = 31 d = 41.

but the output is 

a = 11 b = 20 c = 30 d = 40

in Programming in C edited by
by
832 views

1 Answer

0 votes
0 votes

see precedence of && and | is different thing..  but | is left associative so leftmost of all will be solved first i.e ++a = 11. 

So, TRUE| ++b | ++c&&++d .. short circuit and overall result is true.

O/P- a = 11 b = 20 c = 30 d = 40

2 Comments

Why would left associativity of logical OR operator make it execute first than the higher precedence logical AND operator?!

According to me, because of precedence logical AND will be executed first and then among all logical OR operators the leftmost will be executed first. You can even try out by using brackets. In other words, I agree with the reasoning of Jason.
0
0
In the question, there is logical OR but explain with bitwise OR.
0
0