Strange behavior "+ ="

What should return this code segment? 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;
}

strange, he returns 8 8 8 to me ????: - (

+3
source share
5 answers

For a real understanding of the problem here, try the comp.lang.c FAQ in the sequence point article .

+3
source

*f2+=*f1+=a+=2.5;

Same Old Undefined Behavior.

+3
source

undefined, a . , , .

+3

This behavior is undefined according to specification 6.5 / 2, since you modify an object more than once between a point in a sequence:

Between the previous and the next point in the sequence, the object must have a stored value, changed to most often by evaluating the expression. In addition, the previous value should only be read to determine the value to be saved.

+3
source

It seems to be translated into

*f2 += 2;
*f1 += 2;
  a += 2.5;

and that is +=not as transitive as =.

0
source

Source: https://habr.com/ru/post/1769356/


All Articles