5
volatile int lhs = 1;
int rhs = 2;
int x = 3;

x = lhs = rhs;

Does an assigment operator return the (typeof lhs)rhs ? or Does it return new, just read value of lhs ? It is important to me since lhs is volatile and it can change between assigments.

I couldnt find the anwser at cpprefernce.

5
  • 2
    See chained assignment. Commented May 11, 2023 at 19:29
  • 2
    I think the question here is, suppose lhs is a memory location that, no matter what you write to it, a read always returns 42. Will x in the sample code end up containing 2 or 42? Commented May 11, 2023 at 19:30
  • 3
    Related, possibly duplicate: assignment expressions and volatile Commented May 11, 2023 at 19:30
  • @jarmod: Why should we see that page; what does it tell us about the volatile issue in this question? Commented May 11, 2023 at 19:33
  • 2
    It is very bad practice to write an expression with 4 side effects that are dubiously sequenced in relation to each other. Commented May 12, 2023 at 8:32

1 Answer 1

8

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.
Sign up to request clarification or add additional context in comments.

1 Comment

that's what i wanted to hear. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.