What must this code segment return ? 16 16 16 right ?
int main(int argc,char *argv[])
{
int a=2,*f1,*f2;
f1=f2=&a;
*f2+=*f1+=a+=2.5;
printf("%d %d %d\n",a,*f1,*f2);
return 0;
}
strangely, it returns 8 8 8 to me ???? :-(
What must this code segment return ? 16 16 16 right ?
int main(int argc,char *argv[])
{
int a=2,*f1,*f2;
f1=f2=&a;
*f2+=*f1+=a+=2.5;
printf("%d %d %d\n",a,*f1,*f2);
return 0;
}
strangely, it returns 8 8 8 to me ???? :-(
It's undefined behavior because the value of a
is modified more than once in that string of assignments. So what you might expect is meaningless.
This is undefined behavior according to spec 6.5/2 because you modify an object more than once between sequence point:
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
It seems that it's translated to
*f2 += 2;
*f1 += 2;
a += 2.5;
and that +=
is not so transitive as =
.
For an actual understanding of the issue here try comp.lang.c FAQ article on sequence points.