the ans is -2 2 0 1Code: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; }
BUT pls explain HOW ?
the ans is -2 2 0 1Code: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; }
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.
no ur wrong cause the ans will depend on the evaluation order of this stmt which is unclear to me
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.
"All that we see or seem
Is but a dream within a dream." - Poe
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
the ans is -2 2 0 1Code:int i = -3,j=2,k=0,m; m=++i || ++j && ++k; printf("%d%d%d%d",i,j,k,m);
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.
but here why is && not evaluated before the || operator ? should the && be evaluated and then ||
http://cboard.cprogramming.com/c-pro...ce-%7C%7C.html
Check out that link. They discuss the same problem.
"All that we see or seem
Is but a dream within a dream." - Poe
> 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.
Now, if you had this instead, something different would happen.
> m= (++i || ++j ) && ++k;
Can you work this one out?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Just a note: real programmers don't write lousy code like that. They are explicit in what they intend and use parentheses (and variable names that make sense) to ensure the behavior is as expected.