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); }
This is a discussion on Output of program within the C Programming forums, part of the General Programming Boards category; Hello, When I executed this program I got output -2 3 0 1. Can anyone explain what how this calculation ...
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); }
You should make an attempt at explaining it yourself. Think of what you know about the operators involved.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
I have put this question for help. If you know answer tell it to me.
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.Code:m = ++i && ++j || ++k;
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.
I can see that, and I know the answer. But you should show us that you have attempted to answer the question yourself.Originally Posted by Stuart Dickson
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:
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
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.Originally Posted by xabhi
Last edited by laserlight; 09-04-2010 at 03:01 AM.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Thanks a lot xabhi. Actually where I made mistake was that I was treating negative value as false.
clearly missed these points... thanksIt 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.