Aswin Mohan Aswin Mohan - 1 month ago 19
C Question

Wrong value being printed by printf

I have this piece of C code

#include <stdio.h>

int main(){
int i , j , m , A[5]={0,1,15,25,20};
i = ++A[1];
printf("%d:\n",i);

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

m = A[i++];
printf("%d:\n",m);

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


and It's output is

2:
2:
15:
3 2 15


Shouldn't the printf print the values as 2 , 2, 15 but why is it printing 3 , 2, 15

P.S : I really didn't abuse this code , someone else did (my professor perhaps) and I'm just learning C .

Answer

The line

m = A[i++];

will increment the variable i in-place after it gets the cooresponding value from the array A.