The C standard does not specify this whether lhs is read or not; it permits either behavior.
C 2018 6.5.16 specifies the assignment operators. Paragraph 3 says:
… An assignment expression has the value of the left operand after the assignment,115) but is not an lvalue…
Footnote 115 says:
The implementation is permitted to read the object to determine the value but is not required to, even when the object has volatile-qualified type.
Thus, the implementation is permitted to do either of:
- read the stored value of
lhs (presumably after the side effect of updating it is complete) to determine the value of lhs = rhs;, or
- directly use the value resulting from converting
rhs to the type of lhs.
lhsis a memory location that, no matter what you write to it, a read always returns 42. Willxin the sample code end up containing 2 or 42?volatileissue in this question?