简体   繁体   中英

How do you evaluate z = x- - == y + 1;

given that

int w = 1;
int x = 6;
int y = 5;
int z = 0;
 
z = !z || !x && !y;
printf("%d\n", z);

z = x-- == y + 1;
printf("%d\n", z);

Could someone explain how the line below would evaluate to 1 if x-- is 5 and y+1 is 6?

z = x-- == y + 1;

The expression x-- evaluated to the value of x before being decremented.

So x-- == y + 1 is the same as 6 == 5 + 1 which is true, then the value 1 is assigned to z .

The expression

z = x-- == y + 1

will be parsed as

z = ((x--) == (y + 1))

IOW, you are comparing the result of x-- to the result of y + 1 , and assigning the result of that comparison to z .

The result of x-- is the current value of x ; the side effect is that x is decremented. So, given

x == 6
y == 5

then

x--            == 6
y + 1          == 6
(x-- == y + 1) == 1

so 1 is assigned to z ; after this evaluation, x will equal 5 .

Note that C does not force left-to-right evaluation in this case - y + 1 may be evaluated before x-- . Also be aware that the side effect of the -- operator on x does not have to be applied immediately after evaluation - the whole thing could be evaluated as

t1 <- y + 1
t2 <- x
 z <- t2 == t1
 x <- x - 1

or

t1 <- x
t2 <- y + 1
 x <- x - 1
 z <- t1 == t2

or any other order. The update to x and the assignment to z can happen in any order, even simultaneously (either interleaved or in parallel).

While idiomatic C is often a little head-scratchy, this takes things a bit too far. As an academic exercise for learning about operator precedence and side effects it's okay (not great), but anyone who did this in production code could expect some grief over it in a code review. If nothing else it doesn't scan well - just adding some parens (like I did above) would greatly help with readability.

From the C Standard (6.5.2.4 Postfix increment and decrement operators)

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).

3 The postfix -- operator is analogous to the postfix ++ operator, except that the value of the operand is decremented (that is, the value 1 of the appropriate type is subtracted from it)

Thus in this expression statement

z = x-- == y + 1;

that may be equivalently rewritten like

z = ( x-- ) == ( y + 1 );

the value of the postfix decrement expression x-- is the value of the variable x before decrementing. That is the value of the expression is equal to 6 .

The value of the expression y + 1 is also equal to 6 because the value of y is equal to 5 .

And at last (the C Standard, 6.5.9 Equality operators)

3 The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.108) Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true

So as the value of the expression x-- is equal to the value of the expression y + 1 then the variable z gets the value 1 .

You could get the expected by you result keeping the postfix decrement operator the following way

z = ( x--, x == y + 1 );

by inserting the comma operator. In this case after the comma operator there is a sequence point that means that in second operand of the comma operator x == y + 1 x will be already equal to 5.

On the other hand, if you will write for example the expression like this

z = --x == y + 1;

where instead of the postfix decrement operator there is used unary decrement operator -- then the value of the expression --x will be equal to 5 (the value of the unary decrement operator is the value of its operand after decrementing). In this case the variable z gets the value 0 because 5 is not equal to 6 (the value of the expression y + 1 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM