what is the output of the following c code:
int i=-3,j=2,k=0,m;
m= ++i||++j&&++k;
printf("%d%d%d%d",i,j,k,m);
please also tell the order of evaluation
what is the output of the following c code:
int i=-3,j=2,k=0,m;
m= ++i||++j&&++k;
printf("%d%d%d%d",i,j,k,m);
please also tell the order of evaluation
Write a program to find out. With the output, figure it out, then come back here for a confirmation.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
mam,i know the output. its -2,2,0,1
but i dont know how its coming.
according to me:
since && has higher precedence than ||,that part of the expression will be evaluated first so,
j becomes 3 and k becomes 1
(3&&1)=1(since both are positive).
now i is evaluated so it becomes -2
-2||1 = 1
so m =1
so the output should be -2 3 1 1. but its wrong can you tell me why?
No, precedence determines grouping, not order of evaluation. We could make the grouping more explicit by introducing parentheses:Originally Posted by theju112
Nonetheless, it is the ++i that is evaluated first because of the sequence point introduced by operator||Code:m = ++i || (++j && ++k);
EDIT:
No, theju112 is correct to say that && has a higher precedence than ||.Originally Posted by manasij7479
Last edited by laserlight; 08-12-2011 at 10:13 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
cool!! that makes perfect sense...
but just hav a look at this wiki page:
Operators in C and C++ - Wikipedia, the free encyclopedia
here its given that
&& has precedence 13 and || has 14 that's how i started evaluating the exp.
oh boy its a lot complicated than i imagined.
c++ witch,so ur saying that all side effects of an expression should be executed before a sequence point right?
so i++ is evaluated giving a negative value of -2.since its -ve,the remainder of the expression has to be evaluated .now the part:
++j&&++k are evaluated
so j would be 3 and k becomes 1.
even that is not the correct output.
now what??
hello??????anybody there ??
Did you use google? Bad form to bump threads with "Pay attention to me posts". Short circuit evaluation my friend means that the sequence point introduced by || causes the program to figure out the answer without doing the all the math, essentially. Thus on the example since the left hand side is true, it doesn't bother doing anything with the right hand side.
sorry about that last post guys...
andrewhunter thanks for that perfect answer.
what i thought was that a -ve value is also a false value just like a zero is false in c.
now one last doubt:
a sequence point causes all side effects to be executed without consideration of precedence just as it happened in this example.
am i right?
The misunderstanding you are having is what 'precedence' refers to when it comes to these boolean operators. Take a look at Laser's post #5. That post clearly demonstrates that precedence here refers to grouping not order of evaluation.
ok..that explains it..so precedence is used for grouping of terms in an expression and not for deciding the order in which they are
evaluated.once the terms are grouped the evaluation is done.now am i right?
Basically........
As has been discussed the precedence determines grouping, which basically means it decides where the sequence point is. So in the above example we have x || y && z. Based on precendence the sequence points would be generated as:
x <sequence point> operand2
then you would have
y <sequence point> z.
Now for the evaluation of the sequence points, we have this:
And as we can see, short circuiting is guaranteed by the standard, and thus in this example anything after the first sequence point is not evaluated.Originally Posted by C99 6.5.13/14
i guess this is not beginner stuff as i expected.
here i go again: now im having doubts with that sentence i have bracketed in your re above.
as far as i know a sequence point is generated at && and || operators. so are we really considering precedence here for anything?
now i just want to know this : (x||y)&&z is wrong grouping and the right grouping is : x||(y&&z).is this correct?
after this grouping is done,x is evaluated and it is non-zero.so evaluation stops there and thats it.