what is the value of this expression?

This is a discussion on what is the value of this expression? within the C Programming forums, part of the General Programming Boards category; 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 ...

1. what is the value of this expression?

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

2. Write a program to find out. With the output, figure it out, then come back here for a confirmation.

3. Originally Posted by laserlight
Write a program to find out. With the output, figure it out, then come back here for a confirmation.
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?

4. Originally Posted by theju112
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, they get evaluated in order. (I was wrong in saying that they had the same precedence)
So, a short circuit happens here and the RHS never gets evaluated (seeing that the LHS is already true).

5. Originally Posted by theju112
since && has higher precedence than ||,that part of the expression will be evaluated first
No, precedence determines grouping, not order of evaluation. We could make the grouping more explicit by introducing parentheses:
Code:
`m = ++i || (++j && ++k);`
Nonetheless, it is the ++i that is evaluated first because of the sequence point introduced by operator||

EDIT:
Originally Posted by manasij7479
No the precedence is same
No, theju112 is correct to say that && has a higher precedence than ||.

6. 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.

7. 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??

8. Google "short circuit evaluation in C". This will explain what is going on.

9. hello??????anybody there ??

10. 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.

11. 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?

12. 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.

13. 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?

14. 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:
Originally Posted by C99 6.5.13/14
Logical AND:
Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation;
there is a sequence point after the evaluation of the first operand. If the first operand
compares equal to 0, the second operand is not evaluated.

Logical OR:
Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is
a sequence point after the evaluation of the first operand. If the first operand compares
unequal to 0, the second operand is not evaluated.
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.

15. i guess this is not beginner stuff as i expected.

Originally Posted by AndrewHunter
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.
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.

Page 1 of 2 12 Last
Popular pages Recent additions