plz give an answer,,urgent

//output problm..

#include <stdio.h>
#include <conio.h>

int main()
{
int i=1,l=-2,k=11,j=0,m=1;
if((i++ && l--) || (k-- && j--) && (m++))
{
printf("i=%d l=%d k=%d j=%d m=%d",i,l,k,j,m);
}



getch();
}




// the above code give the output as:
i=2 l=-3 k=11 j=0 m=1



can anyone explain...how this output comes..reply fast! plz..
Last edited on
Because that was the state of your variables by the time your program went to print them out?
hey..thats what im asking here..how?... why has i and l changed..but not k j and m..?? all conditions are true..so all variables should.. change their values...
It's due to short circuit logic in the if statements. If you have [x OR y], and x is true, then it doesn't matter what the y is because the output will be true either way, so it doesn't bother evaluating it.
http://en.wikipedia.org/wiki/Short-circuit_evaluation

Likewise, if you have [x AND y], and x is false, it will not bother evaluating y.

I assume this is just an academic exercise, but please don't do something complicated like this in real code as it makes it really hard to follow.
Last edited on
Topic archived. No new replies allowed.