Output multiple columns and pre-increments in a single expression

I am new to C, so plz sum1 help me. C code written

int i=3; printf("%d",++i + ++i); 

Complier gvs O / P = 9. How?
Thank you in advance

+1
source share
1 answer

The results are undefined. You change a variable more than once in an expression (or a sequence point to be more precise).

Changing a variable more than once between points in a sequence is undefined, so don't do this.

Perhaps this is your compiler, as this particular case solves ++i + ++i as

  • add the last ++i , getting 4, leaving me equal to 4
  • add the first ++i , getting 5, leaving me equal to 5 (since the previous step I left as 4, increasing it to 5)
  • sum two values, 4 + 5.

Another compiler, or if you change the optimization level, or slightly change the code, a different conclusion may arise.

+4
source

All Articles