-1

I have a very basic doubt, I tried the following code in C.

i=(++i)+(++i)+(++i);

I expected the answer to be 6 but the answer came out to be 7. How is it this possible ?increment in C

1 Answers1

4

Assigning multiple times to a single memory location between two sequence points yields undefined behaviour, no certain value or behaviour is to be expected.

Rule of thumb: Do not assign multiple times to a single value within a single expression:

++i; ++i; i+= i; // okay
++i + ++i;       // not okay
Sebastian Mach
  • 37,451
  • 6
  • 88
  • 128