0
int a=10,b=20;
b = a+b-(a=b); 

In this expression why (a=b) is not first operation? if it is performs according to priority then it b has to get 20 itself. But b is getting 10 itself, why? Could any one clarify my doubt?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Ramakrishna
  • 89
  • 1
  • 1
  • 3
  • 1
    Ref. http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points (there is a lot of C++ and other flak there, but good links and reads nonetheless) – user2864740 Feb 18 '14 at 07:19
  • 3
    Don't do weird things please. – Maroun Feb 18 '14 at 07:20
  • 2
    Because you both assign to `a` and reference `a`, you invoke undefined behaviour. Any result you get, including a program crash or a wiped disk, is a legitimate result. – Jonathan Leffler Feb 18 '14 at 07:23
  • 1
    In addition to this being undefined behavior, it is also unspecified behavior. Because you can't know whether the sub-expression `a`, `b` or `a=b` is evaluated first: the order of evaluation of sub-expressions is undefined behavior. So this code is very much broken and _operator precedence_ (what you call "priority") has nothing to do with it. – Lundin Feb 18 '14 at 07:42

1 Answers1

4

This invokes undefined behavior. Anything could be happen. Note that it is sure here that (a=b) evaluates before the subtraction but it does not guarantee that the value of b get assigned to a just after the evaluation. a may get modified after the next sequence point (the ; of the statement here).

The Standard states that

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.

Suggested reading: c-faq Question 3.8

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Why is this undefined behavior? I think this is merely _unspecified_ behavior. – Lundin Feb 18 '14 at 07:28
  • @Lundin; No its UB. In an expression you can't modify a variable if it is used at another place in the same expression. – haccks Feb 18 '14 at 07:30
  • 1
    In C11 they managed to screw that text up so it got harder to read, same meaning: (C11 6.5) "If a side effect on a **scalar object is unsequenced relative** to either a different side effect on the same scalar object or **a value computation using the value of the same scalar object**, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings." Emphasis was mine. – Lundin Feb 18 '14 at 07:35
  • @Lundin; ***or a value computation using the value of the same scalar object,***: this is the point. – haccks Feb 18 '14 at 07:36
  • When you say '`(a=b)` evaluates first', what do you mean? If it is 'before a value is assigned to `b`', then I'd agree; if it is 'before `a+b` is evaluated', then I can't agree. – Jonathan Leffler Feb 18 '14 at 07:49
  • @JonathanLeffler; Edited my answer. In fact evaluation of sub-expression `a`, `b` and `(a=b)` can take place in any order. What I mean that, before subtraction `b=a` get evaluated. – haccks Feb 18 '14 at 08:16