0
int main(){
    int a[5]= {5, 1, 15, 20, 25};
    int i, j, k=1, m;
    i = ++a[1]; // i becomes a[1] + 1 which is 2 but a[1] changes to 2 even though I'm assigning a value to i?
    j = a[1]++; // j becomes a[1]+1 which is 2+1 but stay as 2? 
    m = a[i++]; // because i is 2, shouldn't the output be a[3]? 

    printf("\n%d %d %d", i, j, m);
    return 0;
}

In this code, the output comes out as 3, 2, 15.

How does the increments work specifically? Thank you.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Alex Oh
  • 91
  • 5

3 Answers3

2

The pre-increment operator (++x) increments the value and returns the new value. The post-increment operator (x++) increments the value and returns the old value.

It's easy to see what happens if you decompose the pre-increment and post-increment operators into two different lines.

int a[5]= {5, 1, 15, 20, 25};
int i, j, k=1, m;

// i = ++a[1]; becomes:
++a[1];    // a = {5, 2, 15, 20, 25}
i = a[1];  // i = 2

// j = a[1]++; becomes:
j = a[1];  // j = 2
a[1]++;    // a = {5, 3, 15, 20, 25}

// m = a[i++]; becomes:
m = a[i];  // m = a[2] = 15
i++;       // i = 3

So, in the end:

i = 3
j = 2
m = 15

In particular, in this case:

m = a[i++];

The variable i previously had a value of 2, so the post-increment operator increments i (which becomes 3), but returns 2, so that line is equivalent to:

m = a[i];  // Use old value here
i++;       // Increment after
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
1

1.

i = ++a[1]; 

//i becomes a[1] + 1 which is 2 but a[1] changes to 2 even though i am assigning a value to i?

Correct, i becomes the value of a[1] + 1 and a[1] do so too. Both, a[1] and j have the value of 2 after the expression.


2.

j = a[1]++;

// j becomes a[1]+1 which is 2+1 but stay as 2?

No, j do not get to the value evaluated by a[1] + 1.

After this expression, The value inside of j is still a[1] or better said 2, but the value of a[i] will change to 3 after the expression.

The Postincrement-Operator (variable)++, as opposed to the Preincrement-Operator, ++(variable), increases the value of the operand only after the respective operation:

C: What is the difference between ++i and i++?


3.

m = a[i++]; 

// because i is 2, shouldn't the output be a[3]?

No, because like I said above, The Postincrement-Operator only increments the value of the operand after the operation, so i in m = a[i++]; is still i, not i + 1.

Community
  • 1
  • 1
  • Actually, Robert, that's not right. `a[1]` *does* get incremented, regardless. But, because this is a *pre-*increment case, you get the *incremented* value. Post-increment would have returned the value before it got incremented. – Mike Robinson Jan 31 '20 at 17:06
1

Pre-increment first increments the value, then returns the incremented value to you. Post-increment returns the value before it gets incremented.

The first statement initially sets i=2, but the third statement i++ increments it again. That's why i=3 in the result. j=2 because at the time this was the value of a[1]. m=15 because at the time the statement is executed, i=2 and this is the (zero-based) value of that element in the array.

The code as written does many things which are difficult to decipher ... hence the question. It's not a good idea to write code in this way except as an exercise.

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41