Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Operator associativity
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Right-associativity of assignment operators == In many [[imperative programming language]]s, the [[assignment operator]] is defined to be right-associative, and assignment is defined to be an expression (which evaluates to a value), not just a statement. This allows [[Assignment (computer science)#Chained assignment|chained assignment]] by using the value of one assignment expression as the right operand of the next assignment expression. In [[C (programming language)|C]], the assignment <code>a = b</code> is an expression that evaluates to the same value as the expression <code>b</code> converted to the type of <code>a</code>, with the [[Side effect (computer science)|side effect]] of storing the [[Value (computer science)#lrvalue|R-value]] of <code>b</code> into the [[Value (computer science)#lrvalue|L-value]] of <code>a</code>.{{efn|1=An expression can be made into a [[Statement (programming)|statement]] by following it with a semicolon; i.e. <code>a = b</code> is an expression but <code>a = b;</code> is a statement.}} Therefore the expression <code>a = (b = c)</code> can be interpreted as <code>b = c; a = b;</code>. The alternative expression <code>(a = b) = c</code> raises an error because <code>a = b</code> is not an L-value expression, i.e. it has an R-value but not an L-value where to store the R-value of <code>c</code>. The right-associativity of the <code>=</code> operator allows expressions such as <code>a = b = c</code> to be interpreted as <code>a = (b = c)</code>. In [[C++]], the assignment <code>a = b</code> is an expression that evaluates to the same value as the expression <code>a</code>, with the side effect of storing the R-value of <code>b</code> into the L-value of <code>a</code>. Therefore the expression <code>a = (b = c)</code> can still be interpreted as <code>b = c; a = b;</code>. And the alternative expression <code>(a = b) = c</code> can be interpreted as <code>a = b; a = c;</code> instead of raising an error. The right-associativity of the <code>=</code> operator allows expressions such as <code>a = b = c</code> to be interpreted as <code>a = (b = c)</code>.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)