-2
#include<stdio.h>
int main()
{
  int i=8,j=5,x;
  i++;
  printf("%d,%d,%d,%d",i++,j,x=i+i+++j++,i);
}

output:10,6,24,11 The final value of i is 11 so x should be equal to 26. Why its value is 24?

Fatima Rashid
  • 245
  • 1
  • 3
  • 6

1 Answers1

1

Simple answer - It doesn't, so don't do it :)

From http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf page 10 note 15 : "evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced."

In other words, if you have "ambiguous" statements (like these increments), then the order of operations and evaluations is undefined - i.e. left up to the compiler writer.

So, just another reason to avoid writing ambiguous code in the first place.

racraman
  • 4,988
  • 1
  • 16
  • 16