I am learning programming and I have started from C language. I was reading Let us C book. And I was going through this program in that book.
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 ( "\n%d %d %d", i, j, m ) ;
}
My understanding was, it will print i as 2
, j as 1
and m as 15
But somehow it is printing as i as 3
, j as 2
and m as 15
? Why is it so?
Below is my understanding-
b = x++;
In this example suppose the value of variable ‘x’ is 5 then value of variable ‘b’ will be 5 because old value of ‘x’ is used.
b = ++y;
In this example suppose the value of variable ‘y’ is 5 then value of variable ‘b’ will be 6 because the value of ‘y’ gets modified before using it in a expression.
Is there anything wrong in my understanding?