Search code examples
c#cpostfix-operator

Why is `i += i++` 1 in C and 0 in C#?


Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)

There was a very nice question on Stack overflow.

For i = 0, why is (i += i++) equal to 0?

But when I tried out the same code in C, it gave different results:

int i = 0;          
i += i++;          // 1 in C and 0 in C#
printf("%d", i);

But the following:

i = i++ + i;       // 1 in C and 1 in C#
i += i++ + i;      // 1 in C

In C# it evaluates the ++ and =+ operators, first by assigning tempVar for each fo them and doing the operation on the tempVars. How does C implements it? Or is different by architecture?


Solution

  • The C standard does not specify an order of evaluation. It is left to the compiler implementation.