Question

Why does the following piece of C code print 12 12 12

int main(int argc, char const *argv[]) {
  int a = 2, *f1, *f2;
  f1 = f2 = &a;
  *f2 += *f2 += a += 2.5;
  printf("%i %i %i\n", a, *f1, *f2);
  return 0;
}
Was it helpful?

Solution

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

This line has Undefined Behavior because you change the value of *f2(i.e. a) more than once within the same expression without an intervening sequence point. UB means that your program may print "Hello World", it may crash, it may print 12 12 12 or 12 12 1029 or it may start eating your brains. Don't rely on undefined behavior.

To quote the C++ standard ( I know the question is tagged C, but I don't have a C standard by me and I know the same rule holds in C)

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified.53) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.

OTHER TIPS

It prints out the same value for all because you are only pointing to one int variable: a.

It prints out 12 because a + 2.5 = 4 (a is an int), and then you add it to itself twice.


@Downvoters: Why so negative? I think my answer says what this compiler is doing on this example code, which should help the OP understand the behaviour. I agree that Armen Tsirunyan's answer is right (i.e. should get the tick) and that the behaviour is undefined according to the standard. But standards are implemented, and I've yet to see a compiler that compiles code and then, at run-time, suddenly throws up its hands and says Undefined behaviour!.

I because *f2 and *f1 point to a (an integer).

So *f2 = &a = 2 and *f1 = &a = 2

At this point you add to a the value of 2.5 (because a is an integer, you'll obtain 4).

Than you have

a = 4
f2 = 4
f1 = 4

At this point you do f2+f1+a = 12.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top