# C arrays question

Hi,

Im new to C programming and having trouble with the output of the following C code

#include
void main()
{
int a[5]={5,1,15,20,25};
int i,j,k=1,m;
i=++a[1];
j=a[1]++;
m=a[i++];
printf("
%d %d %d",i,j,m);

I cant understand why is the output 3, 2, 15.
Would appreciate if soemone helps me understanding the logic of the output.

}

• [code]
a[0] = 5
a[1] = 1
a[2] = 15
a[3] = 20
a[4] = 25

i = ++a[1];
j = a[1]++;
m = a[i++];[/code]

i is being assigned the value in a[1]; however, ++a[1] pre increments the value of a[1], so a[1] becomes 2.

j is assigned the value of a[1], which is now 2 from the previous operation (i = ++a[1]); the assignment of j post increments the value of a[1], so a[1] then becomes 3.

m is being assigned the value of a[i++]; i is 2, so you get the value from a[2] or 15; however, a[i++] post increments the value of i as well, so it now becomes 3.

when you're ready the print the results, you've got

i being 3
j being 2
m being 15

If you output your array, you'll see the change to a[1].

[code]
for(z=0; z < sizeof(a)/sizeof(a[0]); ++z) {
printf("[%d] = %d
", z, a[z]);
}[/code]

Hope that helps.
• thanks,its clear!