Would this cause undefined behavior please?

R

rayw

int x,y,z;

x=y=z=1;

z=++x||++y && ++z;

My understanding is that the order of things here are:

++x = x = 2

As x is 'true', the ++y doesn't operate

To evaluate &&, ++z operates, setting z to 2.

true && true, and that assigns 1 to z.

Is this right please?
 
R

rayw

     z=++x||++y && ++z;
means the same thing as:
     z = ++x || (++y && ++z);

To evaluate the result of the || operator,
(++x) is evaluated first, like you said,
but after that, there is nothing more to do.

(++y && ++z) is not evaluated.

Of course! Many thanks.
 
L

lawrence.jones

pete said:
(++y && ++z) is not evaluated.

Which is good because, if it were, you (the OP, not pete) would be
trying to set the value of z twice in the same expression without a
sequence point between them, which is undefined behavior, just like:

z = ++z;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top