What is the output of the following program?main() { int i=-1,j=-1,k=0,l=2,m; m=i++ && j++ && k++ | | l++; printf("%d %d %d %d %d",i,j,k,l,m); }

The output is


0 0 1 3 1


This is as follows


First the expression gets reduced as


m = -1 && -1 && 0 || 2


This is because ++ gets the higher precedence and being postfix operator it is first applied to expression and then only incremented. Now in this Logical AND gets higher precedence over Logical OR and the associativity or the order in which Logical AND as well as Logical OR gets executed is left to right


So -1 && -1 && 0 gives result as 0


This Logical OR with 2 namely m = 0 || 2 gives value as 1


So value of m is 1.


Now the expression is evaluated so the variables i, j, k, l gets incremented and all gets printed resulting in output as


0 0 1 3 1

Questions by GeekAdmin   answers by GeekAdmin

Showing Answers 1 - 4 of 4 Answers

Deepak

  • May 15th, 2007
 

&& and ||, they are called logical operator which takes two operands. The result of this operator will be either zero or one  (i.e any non-zero value is equal to one)

so in this case
0 || 2
nothing but  zero || non-zero  = one

  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