-2

Associativity of Operators Question Continued: In C programming Language: For the Question below: What order would 3 (Number 3) be assigned to the variables? As in which variable would receive 3 first, second and third? And which variable would have 3 in the end?

Question: A = B = C = 3

Further explanation of What I'm asking/My attempts to understand this concept:

According to the image I've attached stating the Associativity of Operators, the Assignment operators should be from left to right no?

So should 3 be assigned to A, then B, and then C?

According to a practice question solution it is the opposite, 3 being assigned to C, then B, then A, so am very confused why it's right to left? When the Associativity of Operators say it's left to right!

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 3
    I can't see your image, but assignment operators have [right-to-left associativity](https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence). – Fred Larson Mar 25 '20 at 15:29
  • I would ordinarily expect that the assignments would be applied "inner to outer," hence `C, B, A,` and that after this is done all three will contain `3`. However, I would never want to *write code* that is based on any such obscurity, and so I really don't know what this "rather-trick question" is really supposed to be about. – Mike Robinson Mar 25 '20 at 15:29
  • The code is equivalent to `A = (B = (C = 3))`. All three get the value `3`. – Weather Vane Mar 25 '20 at 15:30
  • So, you want `A` to be assigned to `3`?! – machine_1 Mar 25 '20 at 15:30
  • 3
    I can see his image, Fred, and *it* says that assignments are left-to-right. Pretty certain that's wrong. But ... this whole darned question is obscure to the point of irrelevancy, IMHO. "Bad test-writer! NO biscuit!" – Mike Robinson Mar 25 '20 at 15:31
  • 3
    Sahil, where is that picture from? It seems to contain _wrong_ information. – Fildor Mar 25 '20 at 15:33
  • 2
    I'm not sure I understand why it even matters. Without some other thread trying to access these variables at the same time (and thus leading to data races and undefined behavior), you'll never see an intermediate state where some of the variables were assigned and the rest weren't. – Kevin Mar 25 '20 at 15:33
  • 2
    MS's page for [Precedence and associativity of C operators](https://learn.microsoft.com/en-us/cpp/c-language/precedence-and-order-of-evaluation?redirectedfrom=MSDN&view=vs-2019) gives *"Simple and compound assignment - **Right to left**"* – Weather Vane Mar 25 '20 at 15:35
  • 2
    [cppreference agrees](https://en.cppreference.com/w/c/language/operator_precedence), assignment operators are right-to-left. – Fred Larson Mar 25 '20 at 15:36
  • machine_1, I'm not trying to assign A to 3. I know in the end 3 gets assigned to A. I was just confused in terms of the solutions to a C program that was presented mentioned the order is from left to right, but it made sense to me that it should be right to left. So was trying to understand that, but according to most above it's right to left. So it was simply a wrong solution given to me of a c practice problem. – Sahil Mahajan Mar 25 '20 at 15:38
  • 1
    @Kevin I guess it's more of an academic question rather than a "real-life" thing: _"According to a practice question ... "_ – Fildor Mar 25 '20 at 15:38
  • 2
    Not sure that this question deserves 5 downvotes; OP will learn to be more discriminating in sources, but given the information OP possesses, this is a reasonable question. – ad absurdum Mar 25 '20 at 15:39
  • If this is from some training / school material, you should notify the author, if it is not already mentioned in some "errata" doc. – Fildor Mar 25 '20 at 15:42
  • @Fildor -- "_more of an academic question_": assignment statements like this are usually considered bad style, but still they exist in real world code. Whether or not rules of associativity and precedence matter for _assignment_, they matter for other operators; OP noticed a contradiction and seeks clarification about associativity and precedence. – ad absurdum Mar 25 '20 at 15:46
  • @exnihilo I was referring to OP's statement _"According to a practice question ..."_. Hence my interpretation that this is in an academic setting. Outside that, you are absolutely right, of course. – Fildor Mar 25 '20 at 15:48
  • The table in the posted image is wrong. C assignment operators (`=`, `+=`, etc.) group right-to-left. They can't possibly group left-to-right. If they did, then `(a = b) = c;` would work, which it doesn't. On the other hand, `a = (b = c);` does work, as exptected. I suggest finding some better documentation. – Tom Karzes Mar 25 '20 at 15:49
  • is it possible that the behavior depends by the specific compiler used? In this case, you can have an answer only by reading what is doing you compiled code (just look the assembly). Can it make sense? – Leos313 Mar 25 '20 at 16:07

1 Answers1

4

The expression A = B = C = 3 is parsed in C as A = (B = (C = 3)). The assignment operator associates right-to-left.

However, the actual assignment is specified as a side effect of the expression, and the order in which these side effects occur is not specified by the C standard.

The image in the question is wrong to show the order of assignment operators as left to right, and the source of the image should be regarded with suspicion. The association of assignment operators arises out of the grammar rules in the C standard, where 6.5.16 shows one rule as:

assignment-expression: unary-expression assignment-operator assignment-expression

The fact that the right operand is an assignment expression means that in X = Y, Y can be another assignment expression, such as Z = 4, but X cannot be. So A = B = C = 3 must be parsed as C = 3 being an assignment expression inside of B = …, and B = C = 3 must be an assignment expression inside of A = …. Contrast this with a rule for one of the additive operators in C 6.5.6:

additive-expression: additive-expression - multiplicative-expression

In that rule, the additive-expression is on the left, so A - B - C necessarily groups as (A - B) - C.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • is it possible that the behavior depends by the specific compiler used? In this case, you can have an answer only by reading what is doing you compiled code (just look the assembly). Can it make sense? – Leos313 Mar 25 '20 at 16:07
  • @Leos313: A compiler may define the order (that is, it is permitted by the C standard). I am not aware of any that do, and what a compiler chooses to do may depend on circumstances, such as how the order affects optimizations in regard to the surrounding code. – Eric Postpischil Mar 25 '20 at 16:12