Main(){ int i=1,j=1,k=0,l=1,m; m=++i && ++j && ++k || ++l; Printf("%d %d %d %d %d",i,j,k,l,m);}

Questions by kuttislg

Showing Answers 1 - 6 of 6 Answers

The answer will be i=2,j=2,k=1,l=1,m=1
The expression will be like this
m=(++i&&++j)&&(++k||++l);

First time i and j will be incremented that is (2,2) and logical AND(&&) condition will be performed that is true means 1.

In second bracket k will be incremented that will be 1 after that logical OR(||) is there first is true then no need to go after logical OR( in logical OR in two values one value is 1 then the result will be true always) in that case l will be not incremented and the second bracket is also true both the brackets are giving the true value after performing logical AND(&&) operation it will be also true then 1 will be assigned to m.

  Was this answer useful?  Yes

Output will be 2 2 1 1 1.

Both && and || force left-to-right evaluation; the LHS will be completely evaluated and all side effects applied before the RHS is evaluated. 

If the operator is && and the result of the LHS expression is non-zero, then the RHS is evaluated.  If the operator is || and the result of the LHS expression is 0, then the RHS is evaluated.  Otherwise the RHS is not evaluated. 

a && b evaluates to 1 if both a and b are non-zero, 0 otherwise.
a || b evaluates to 0 if both a and b are 0, 1 otherwise.

The operator && has higher precedence than || and has left-to-right associativity, so the expression "++i && ++j && ++k || ++l" is parsed as "((++i && ++j) && ++k) || ++l".

Evaluation steps:

1.  ++i is evaluated.  The result is 2, so...
2.  ++j is evaluated.  The result is 2, so...
3.  The result of ++i && ++j is 1, so...
4.  ++k is evaluated.  The result is 1, so...
5.  The result of (++i && ++j) && ++k is 1, so...
6. ++l is not evaluated, so...
7.  The value of l (1) is unchanged, and...
8.  The result of ((++i && ++j) && ++k) || ++l is 1, so...
9.  m is assigned the value 1. 

Thus, i = 2, j = 2, k = 1, l = 1, and m = 1. 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions