# 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,1,15,20,25};
int i,j,k=1,m;
i=++a;
j=a++;
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 = 5
a = 1
a = 15
a = 20
a = 25

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

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

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

m is being assigned the value of a[i++]; i is 2, so you get the value from a 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.

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

Hope that helps.
• thanks,its clear!