# Thread: what will be the output of this code snippet

1. ## what will be the output of this code snippet

Code:
```int main(){
int i = -3,j=2,k=0,m;
m=++i||++j&&++k;
printf("%d%d%d%d",i,j,k,m);
return 0;
}```
the ans is -2 2 0 1

BUT pls explain HOW ?

2. Originally Posted by devilofwar
Code:
```int main(){
int i = -3,j=2,k=0,m;
m=++i||++j&&++k;
printf("%d%d%d%d",i,j,k,m);
return 0;
}```
the ans is -2 2 0 1

BUT pls explain HOW ?
I think you get -2 because I is I++ which adds 1. J is declared at top, same with J. M is orignally declared as 0. but its got a ++ increment so 1 is added to it.

Could someone confirm if im right ? if so it proves my revision for my programming test on wednesday is going well lol.

3. no ur wrong cause the ans will depend on the evaluation order of this stmt which is unclear to me

4. Originally Posted by devilofwar
Code:
```int main(){
int i = -3,j=2,k=0,m;
m=++i||++j&&++k;
printf("%d%d%d%d",i,j,k,m);
return 0;
}```
the ans is -2 2 0 1

BUT pls explain HOW ?
First the || has the left-to-right precedence. so you increase i first to get i =-2. since it's an 'or' statement, and you're not comparing ++i with anything, so ++i is true, it won't look at the second expression on the right which icnreatement j and k; thus j and k retains their original values. Then, since the logical test is true, the test return "1", which is then assigned to m, thus m gets the value of 1.

5. even i thought of this explanation But have u considered the fact that && gets evaluated first and then || gets evaluated
Precedence table frm anywhere says this

6. Originally Posted by nimitzhunter
First the || has the left-to-right precedence. so you increase i first to get i =-2. since it's an 'or' statement, and you're not comparing ++i with anything, so ++i is true, it won't look at the second expression on the right which icnreatement j and k; thus j and k retains their original values. Then, since the logical test is true, the test return "1", which is then assigned to m, thus m gets the value of 1.
even i thought of this explanation But have u considered the fact that && gets evaluated first and then || gets evaluated
Precedence table frm anywhere says this

7. Code:
```int i = -3,j=2,k=0,m;
m=++i || ++j && ++k;

printf("%d%d%d%d",i,j,k,m);```
the ans is -2 2 0 1

Ok... this is not bitwise math... so...

The first thing on the right of the equals sign is ++i ... i now equals -2
The second step is the logical OR... which is completed ...
++i succeeded and evaluates to TRUE thus m = 1
Because the first OR is true, the remainder of the statement is never executed so the values of j and k are unchanged.

8. but here why is && not evaluated before the || operator ? should the && be evaluated and then ||

9. Originally Posted by devilofwar
but here why is && not evaluated before the || operator ? should the && be evaluated and then ||
It can't be... or the values of j and k would be changed.

Evaluation ends when the OR clause succeeds.

10. http://cboard.cprogramming.com/c-pro...ce-%7C%7C.html
Check out that link. They discuss the same problem.

11. > But have u considered the fact that && gets evaluated first and then || gets evaluated
But it doesn't.

Since you mentioned precedence, let's put some extra ( ) in for clarity.
> m=++i || ++j && ++k;
becomes
> m=++i || (++j && ++k);

Now regardless of anything else, the boolean operators ALWAYS evaluate the left term first, so ++i is going to happen first. Since that evaluates to true (non-zero), that's the end of the show. The LHS is true, therefore the whole thing MUST be true and the whole RHS of (++j && ++k) is skipped.

> m= (++i || ++j ) && ++k;
Can you work this one out?

12. Originally Posted by Salem
> But have u considered the fact that && gets evaluated first and then || gets evaluated
But it doesn't.

Since you mentioned precedence, let's put some extra ( ) in for clarity.
> m=++i || ++j && ++k;
becomes
> m=++i || (++j && ++k);

Now regardless of anything else, the boolean operators ALWAYS evaluate the left term first, so ++i is going to happen first. Since that evaluates to true (non-zero), that's the end of the show. The LHS is true, therefore the whole thing MUST be true and the whole RHS of (++j && ++k) is skipped.