There is the following C code:
#include <stdio.h>
int main ()
{
int a[5] = {0, 1, 2, 3, 4};
int* a_p = a;
float x = 1.5;
while(x > (*a_p++))
{
printf("*a_p = %d\n", *a_p);
}
printf("*a_p = %d", *a_p);
return 0;
}
The question would be which is the result of the final printf
statement?
I would judge that the order is:
1) inside while
, a_p
address is incremented => *a_p
is 1 (a[1]
)
2) 1.5 is compared with 1
3) inside while
, a_p
address is incremented again => *a_p
is 2 (a[2]
)
4) 1.5 is compared with 2
5) 2 is printed for *a_p
I have tried with 3 compilers and the result is 3.
Why 3 is the correct result? Is first done the comparison and then the pointer is incremented meaning that at step 4 from above, after the comparison is done, *a_p
is 3?
Is this always the behavior (is this behavior defined) or is compiler dependent?