0

I tried this example of array and post increment/ pre increment on it's elements

#include<stdio.h>

int main()
{
    int j[5] = {5, 1, 15, 20, 25};
    int k , l, m, n;

    n = ++j[1];
    k = ++j[1];
    l = j[1]++;
    m = j[k++];

    printf("\n%d, %d, %d, %d", n, k, l, m );

    return 0;
}

here the output is :

2, 4, 3, 20

and if i change the order of n and k ie instead of

n = ++j[1];
k = ++j[1];

i write

k = ++j[1];
n = ++j[1];

The output becomes :

3, 3, 3, 15

I tried this on mingw compiler on windows10 and also on Kali Linux's GCC... Same problem.

It is just like taking different variable name alters the output of program. What might be the cause?

Thanks Everyone for helping me out with this question.

I didnt take the last post increment of k under consideration.

The results would be same of i would have changed

m=j[k++]

with

m = j[n++]
  • You might want to print the value of `j[1]` after each of those statements. – user3386109 Mar 15 '19 at 06:33
  • In the first, n=2 and k=3 after those two lines; in the second, k=2 and n=3. Later, this new value for k will affect the value assigned to m. Why do you find that unexpected? – jhnc Mar 15 '19 at 06:34

3 Answers3

2

Why would not they? The values of k, n and m are depending on their placement in the code.

For example,

 m = j[k++];

this will be affected by the current value of k.

To add a bit about pre and post increment operator, quoting C11,

  • Chapter §6.5.3.1, pre-increment

    The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. [...]

  • Chapter §6.5.2.4, post-increment

    The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it). [....]

I have added some print statements, to check how the values get affected after every statement. You can additionally print the whole array to visualize the change even more.

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

printf("j[1] = %d\n", j[1]);

k = ++j[1];
printf("j[1] = %d, k = %d\n", j[1], k);
n = ++j[1];
printf("j[1] = %d, n = %d\n", j[1], n);
l = j[1]++;
printf("j[1] = %d, l = %d\n", j[1], l);
m = j[k++];
printf("m= %d, k = %d\n", m, k);

printf("\n%d, %d, %d, %d", n, k, l, m );

and the output is:

j[1] = 1
j[1] = 2, k = 2
j[1] = 3, n = 3
j[1] = 4, l = 3
m= 15, k = 3

3, 3, 3, 15
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

In the first case:

n = ++j[1]; //j[1] is incremented and is now 2, so n is 2
k = ++j[1]; //j[1] is incremented again and is now 3, so k is 3
l = j[1]++; //j[1] is incremented yet again and is now 4, but l is 3 as it is post-increment
m = j[k++]; //m  is j[3] which is 20, and k will become 4 as it is post-incremented

So output of n, k, l, m will be 2, 4, 3, 20

In the second case:

k = ++j[1]; //j[1] is incremented and is now 2, so k is 2
n = ++j[1]; //j[1] is incremented again and is now 3, so n is 3
l = j[1]++; //j[1] is incremented again and is now 3, but l is 3
m = j[k++]; //m  is j[2] which is 15, and k will become 3 as it is post-incremented

So output of n, k, l, m will be 3, 3, 3, 15

P.W
  • 26,289
  • 6
  • 39
  • 76
0

Actually, it is not that complicated int j[5] = {5, 1, 15, 20, 25};

n = ++j[1];
// n=2 j[1]=2

k = ++j[1];
// k=3 j[1]=3

l = j[1]++;
// l=3 j[1]=4

m = j[k++];
// since k=3, k becomes 4 but m is assigned j[3] which is 20

Let's see the other case

int j[5] = {5, 1, 15, 20, 25};

k = ++j[1];
// k=2 j[1]=2

n = ++j[1];
// n=3 j[1]=3

l = j[1]++;
// l=3 j[1]=4

m = j[k++];
// since k=2, k becomes 3 but m is assigned j[2] which is 15
Soner Say
  • 106
  • 5