I am reviewing a code with the following snippet for minimal reproducible example. So here the code runs an iteration to initialize an array done
as follows:
#include <stdio.h>
int i,j;
N=10;
int done[500];
void main() {
done[0]=done[N]=1;
for(i=0;i<N+1;done[i++]=0){
printf("Done val %d iteration %d\n",done[i],i);
}
}
The thing I am concerned with that I purpossely initialized the values of done[0]=done[1]=1
However when I run the initialization loop, the values at index 0 and 10 remains unchanged. I would like to understand how is the syntax of done[i++]
actually evaluated?
The increment step of a for
loop takes place after the loop body is executed. It's equivalent to the following code:
i = 0;
while (i < N+1) {
printf("Done val %d iteration %d\n",done[i],i);
done[i++] = 0;
}
As you can see, it's printing the value of done[i]
before it changes it. So it prints the original value.