The Gateway to Computer Science Excellence
+2 votes
141 views
#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 by Active (1.5k points) | 141 views

1 Answer

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

by Active (2.3k points)
0
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
In the question, there is logical OR but explain with bitwise OR.
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,385 answers
198,558 comments
105,371 users