0

At this link, it is stated that

The order of evaluation of sub-expressions, including operands of operators (e.g., +, -, =, * , /) is Unspecified.

It is clear that in the expression

 foo1() = foo2() = foo3() = 7; // snippet of C++ code

the operands of the = operator can be evaluated in any order. Your compiler is free to choose (link, answered by Ralph Tandetzky).

This question may look foolish but I am really confused about this. Is the above statement also true if foo1(), foo2() and foo3() are replaced by a, b, and c? As in:

a = b = c = 7;
Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 3
    I think `foo3() = 7` should be lvalue error whereas `a = b = c = 7;` is perfectly correct. Not?? – Grijesh Chauhan Jul 03 '13 at 20:00
  • 1
    A function call cannot appear as the left hand side of an assignment in C -- but it can be *part of* the LHS. For example: in `arr[foo1()] = arr[foo2()] = arr[foo3()] = 7;`, the three function calls can occur in any of 6 possible orders. The linked example is C++, not C; in C++, functions can return references, which can be assigned to. – Keith Thompson Jul 03 '13 at 20:04
  • @KeithThompson; I have provided link with it.This example is taken from there. This an snippet of the code. – haccks Jul 03 '13 at 20:06
  • Yes, but as I said in my updated comment, the code is valid in C++, not in C. – Keith Thompson Jul 03 '13 at 20:06
  • @KeithThompson; yes true. – haccks Jul 03 '13 at 20:07

1 Answers1

3

It is exactly the same situation. The only difference is that in your second example, a, b, and c have no side effects. No matter which way your compiler's implementation decides to evaluate them, the result will still be the same since none of the evaluations does anything.

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
  • You mean to say that compiler may evaluate `a` first then `7` then may evaluate `c` and then `b`? – haccks Jul 03 '13 at 20:03
  • 1
    @haccks: Yes, but the evaluation of `a`, `b`, and `c` in this context does nothing more than identify the objects to which those names refer, so the order of evaluation doesn't matter (unless perhaps one or more of them are declared `volatile`). – Keith Thompson Jul 03 '13 at 20:06
  • I don't think this would even qualify as a `volatile` access, since there's no lvalue-to-rvalue conversion. – aschepler Jul 03 '13 at 20:09
  • @KeithThompson Two last questions, what does evaluation of `7` means? When will it assigned to operands? – haccks Jul 03 '13 at 20:10
  • 1
    Evaluation of the subexpression `7` simply yields the `int` value `7`. It will be assigned some time before the end of the execution of the statement. – Keith Thompson Jul 03 '13 at 20:13
  • @KeithThompson; I wish your comments should be an answer for this question. – haccks Jul 03 '13 at 20:16
  • @KeithThompson; One more question :(, Is there any rule about assigning the value `7`, before the end of the execution, to the variables? – haccks Jul 03 '13 at 20:41
  • 1
    @haccks All operands may be evaluated in any order, but the order in which the whole expression itself is interpreted is well-defined, since it depends on operator precedence and not order of evaluation. Since the assignment operator has right-to-left associativity, the compiler must interpret the expression as `a = (b = (c = 7));`. – Lundin Jul 03 '13 at 20:47
  • @haccks: The value can be stored any time after the evaluation of `7` (which is trivial) *and* after the determination of the object to be assigned to, and before the next sequence point (in this case, the end of the execution of the current statement and the beginning of the execution of the following statement). In a simple case like `a = b = c = 7;`, none of this really matters; the value 7 will be stored in `a`, `b`, and `c`. Is there some particular reason why this matters to you? – Keith Thompson Jul 03 '13 at 20:49
  • @KeithThompson; Yes Sir. Actually I am confused very much about Order of evolution, Precedence and Associativity. From last night I am searching about this on net,Books. Visiting some sites it is clear to me that order of evolution is independent of others two. I was thinking that this would be my last question on this topic but alas! post by @Lundin again confused me to think about Is there any relation between Precedence and associativity? – haccks Jul 03 '13 at 21:07
  • @KeithThompson; OK, let me check. – haccks Jul 03 '13 at 21:45
  • Allow me to point out, please, that there are differences here between C and C++, that the first example in your question is valid C++ but not valid C, and that you need to clearly define exactly which language you're interested in before you can get a properly meaningful answer. (Also that many websites and programming resources confuse the two, or deal with some odd thing called "C/C++"...) – This isn't my real name Jul 03 '13 at 22:04
  • @ElchononEdelson; You are right,but I have mentioned this in the question as comment `//snippet of C++ code`. And yes currently I am interested only in C – haccks Jul 04 '13 at 01:16