-1
int a[]={1,2,3,5};
    int i=1;
    a[++i]=a[i];
    int j;
    for(j=0;j<4;j++)
    {
        printf("%d",a[j]);
    }



output:1235;

why the output is 1225 and not 1335.

I executed this program on codeblocks. In a[++i]=a[i], Right to left assignment will be their,leading to a[2]=a[1]. Correct me if i am wrong.

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • To get 1335, you would have to change the code to well-defined C code: `++i; a[i-1]=a[i];`. – Lundin Nov 08 '13 at 09:43

4 Answers4

8

Because a[++i]=a[i]; is undefined behavior.

A sequence point is a point in time at which the dust has settled and all side effects which have been seen so far are guaranteed to be complete. The sequence points listed in the C standard are:

at the end of the evaluation of a full expression (a full expression is an expression statement, or any other expression which is not a subexpression within any larger expression); at the ||, &&, ?:, and comma operators; and at a function call (after the evaluation of all the arguments, and just before the actual call).

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
Sadique
  • 22,572
  • 7
  • 65
  • 91
  • Also, in terms of actually implemented behaviour, the compiler is almost certainly processing the left hand side first, so effectively it is expanding it to `++i; a[i]=a[i];` – Oliver Matthews Nov 08 '13 at 09:22
  • I think this might depend on compiler optimization, therefore may vary. – phoxis Nov 08 '13 at 09:25
  • Yeah, it is certainly not something you can rely on; just an observation on how it is being treated here. – Oliver Matthews Nov 08 '13 at 09:26
2
a[++i]=a[i]; // this is undefined

If you only want to change single element of the array ... do it by referencing it directly:

int a[]={1,2,3,5};
int i=1;

a[i]++; // this will increment the ith element of the array by 1

int j;
for(j=0;j<4;j++)
{
    printf("%d",a[j]);
}

Output:

1335

sukhvir
  • 5,265
  • 6
  • 41
  • 43
1

a[++i]=a[i]; is undefined behavior. Because according to C99 section 6.5 paragraph 2

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

= is not a sequence point. Check annex C.

You are modifying the value i one time, but "the prior value shall be read only to determine the value to be stored" is violated as you do a[++i].

Check Footnote 73) for an example of what the paragraph says.

73)This paragraph renders undefined statement expressions such as
i = ++i + 1;
a[i++] = i;

while allowing
i = i + 1;
a[i] = i;

Therefore what the outcome will be cannot be determined. For different run and/or across different computers you can get different results. Such kind of expressions should not be used in C programming.

phoxis
  • 60,131
  • 14
  • 81
  • 117
  • Though please note that footnotes in ISO standards are not normative, they are just comments about the normative text. So citing a footnote for educational purposes (as in this case) is just fine, but citing one to provide a reference or prove a point is not. – Lundin Nov 08 '13 at 09:30
  • This is not to prove a point, this is to help get an idea about what the paragraph tells. – phoxis Nov 08 '13 at 09:32
0

a[++i] = a[i] is undefined behavior. look-up this presentation.

elyashiv
  • 3,623
  • 2
  • 29
  • 52