edited by
819 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

edited by

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

Related questions

1 votes
1 votes
1 answer
1
nish kim asked Sep 2, 2017
5,352 views
{double b =5 & 3 && 4 || 6 | 6 ;printf("%lf",b);}a.2.000000b.1.0c.1.000000d.compilation error
2 votes
2 votes
3 answers
3
radha gogia asked Mar 19, 2018
1,069 views
If I have the grammar :E->E*F | F+E |FF->id ,Now here although + and * are defined at the same level , with different associativity but this grammar produces 2 different ...
2 votes
2 votes
1 answer
4
saipriyab asked Nov 28, 2017
4,604 views
The output of the following programmain ( ){int a = 1, b = 2, c = 3;printf (“%d”, a+ = (a+ = 3, 5, a));}