1. ## Output of program

Hello,

When I executed this program I got output -2 3 0 1. Can anyone explain what how this calculation is reached in program below. Thanks

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

2. You should make an attempt at explaining it yourself. Think of what you know about the operators involved.

3. I have put this question for help. If you know answer tell it to me.

4. Code:
`m = ++i && ++j || ++k;`
the logical operators && and || are short-circuit operators with left to right associativity. so in above expression, evaluation starts from the left most variable, i.e. i, and goes left till the truth value of the expr is not determined. as soon as the truth value is found, the furthur evaluation of expression stops.

so in above expression, we have

m = (++i) && (++j) || (++k)

starting from left we first evaluate the && along with it's operands. in C, any non-zero value is logically true. therefore (++i) && (++j) evaluates true, as their values during comparison can never be zero due to their initial values, and the truth value is also determined for the whole expression ( true || 0 == true). so no further evaluation takes place and the statement ++k is not executed as control never reaches that statement.

note that during the comparison the exact values of i and j cannot be determined as in an expression, only precedence and associativity rules are guaranteed, not the order of evaluation of sub-expressions like ++i. however, after the above expr, the values of i and j have been incremented as they were evaluated, even if their order was not known.

finally, m is set to the logical result of the expression, which is equal to 1.

5. Originally Posted by Stuart Dickson
I have put this question for help. If you know answer tell it to me.
I can see that, and I know the answer. But you should show us that you have attempted to answer the question yourself.

In particular, read the homework guidelines. If you keep getting spoonfed answers like what xabhi did for you, then you are just wasting your time (and ours). You should be learning how these things work from books and tutorials, and then making use of these examples to see if you can apply your knowledge to deepen your understanding. Where we can best help you is where you have some gap or misunderstanding that can be corrected.

EDIT:
Originally Posted by xabhi
starting from left we first evaluate the && along with it's operands.
Actually, because && introduces a sequence point the left hand side, i.e., ++i, is evaluated first. Since the result is non-zero, the right hand side of the expression is evaluated.

Originally Posted by xabhi
note that during the comparison the exact values of i and j cannot be determined as in an expression, only precedence and associativity rules are guaranteed, not the order of evaluation of sub-expressions like ++i. however, after the above expr, the values of i and j have been incremented as they were evaluated, even if their order was not known.
It is generally true that the order of evaluation of subexpressions is unspecified, but in this case we can determine the order of evaluation by inspection because of the sequence points that are introduced.

6. Thanks a lot xabhi. Actually where I made mistake was that I was treating negative value as false.

7. It is generally true that the order of evaluation of subexpressions is unspecified, but in this case we can determine the order of evaluation by inspection because of the sequence points that are introduced.
clearly missed these points... thanks