code 1:
i = 0;
while ( arr[i++] != EOF )
printf("%d", i);
output: 1 2 3 4
code 2:
i = 0;
while ( arr[i] != EOF )
{
printf("%d", i);
i++;
}
output: 0 1 2 3
In the above code, arr[] = "//\n\0"
However, I assume i++ should first assign i and increment right?
i.e.,
if i = 1
j = i++
j = 1 (Not 2)
So, what exactly is happening in code 1 and why it isn’t behaving like code 2?
You increment
iwhen you do thisarr[i++], so when you go to print it out of course it will be incremented already.Instead print out the array element so you know you got the right one: