简体   繁体   中英

Are postfix and prefix increment and decrement expressions lvalues in C11?

Are postfix and prefix increment and decrement expressions lvalues in C11? Usually the standard has notes at the bottom specifying whether or not a given expression is an lvalue. I can not find anything in the standard in this case.

I know that most of the time there would be sequence point issues, but there are some edge cases where it seems like it could be useful to know this. For example, in 6.5.2.4:

Postfix ++ on an object with atomic type is a read-modify-write operation with memory_order_seq_cst memory order semantics.

It follows that with an atomic type, something like ++x=x+y will be a pithy way to do something. Not that it would be important to be able to do such a thing, I just don't like not knowing things.

This aspect of behavior of postfix and prefix increment and decrement operators is specified through the portion of standard document dedicated to additive operators and assignment operators.

In case of postfix operators

[...] See the discussions of additive operators and compound assignment for information on constraints, types, and conversions [...]

In case of prefix (unary) operators

[...] The expression ++E is equivalent to (E+=1) [...]

(The latter is more direct than the former, but the intent with regard to lvalue-ness of the result is the same.)

Assignment expression in C is not an lvalue

An assignment expression has the value of the left operand after the assignment, but is not an lvalue.

As a side note, it is one of the deep fundamental differences between C and C++ languages: C++ is an lvalue-preserving language, while C is an lvalue-discarding language.

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