-1

I have this expression:

B[i]=B[++i]*i;

In C this is undefined?

What happen first? is evaluated the right of the assignment symbol first, or the left?

exsnake
  • 1,767
  • 2
  • 23
  • 44
  • It's undefined. And if you can't figure out what it means, why would you ever want to write code like that? – Bill Lynch Oct 16 '14 at 20:44
  • 1
    @BillLynch is question from a test... they ask about what is the output of a program, and the code have that expression. For me is undefined what happen, and i can I'm right, Thanks... – exsnake Oct 16 '14 at 20:45
  • 1
    So, the test either expects UB as the answer, or it is rubbish. I give ten-to-one odds for the latter. – Deduplicator Oct 16 '14 at 20:46
  • @exsnake -the only answer is "yes, it is undefined." – Leonardo Herrera Oct 16 '14 at 20:47
  • Also somewhat related to: [Assignment operator sequencing in C11 expressions](http://stackoverflow.com/q/22616044/1708801) ... the defect report I cite in my answer gives a good framework for evaluating these type of constructs. If you can understand that proof it will help you understand a lot of these cases. – Shafik Yaghmour Oct 16 '14 at 20:51
  • The order of evaluation is *unspecified*, so any expression that attempts to modify a variable more than once or tries to read from and modify a variable between sequence points will give different results for different platforms, compiler settings, etc. The language definition specifically calls this out as *undefined behavior*, meaning the compiler isn't required to handle it any particular way. – John Bode Oct 16 '14 at 20:54
  • @JohnBode well it is both unspecified and undefined behavior, see [my answer](http://stackoverflow.com/a/18260171/1708801) to the link duplicate. In this case they are orthogonal issues, the unspecified order of evaluation does not make it undefined, the multiple modifications do. – Shafik Yaghmour Oct 17 '14 at 00:54

1 Answers1

0

It is undefined and can be evaluated in any order.

kraskevich
  • 18,368
  • 4
  • 33
  • 45