I am a newbie to C programming. Now, I am trying to understand the topic "C Operator Precedence and Associativity". Especially, I would like to pay attention to the order of evaluation with parenthesis, and why the didn't evaluate at first. I write the next example:
#include <stdio.h>
int main() {
volatile int i = 10;
i = i + i + i++ + (i *= 2);
printf("%d\n", i); //52
return 0;
}
As I understand from the book "The C Programming Language. 2nd Edition (THE ANSI C)", each operator has specific precedence and associativity. [C table](https://i.stack.imgur.com/LP8eY.jpg)By the rules, this expression must have the next evaluation order:
- First precedence has "i++", it must be evaluated at first;
- Second precedence has "(i *= 2)";
- And then the summation;
But why the compiler evaluates the expression in this order:
1(i) + 2(i) + 3(i++) + 4(i *= 2);
Explain me please.
Result is 52.
Expecting:
- (i *= 2), return 20 // i = 20
- i++, return 20 // i = 21
21 + 21 + 20 + 20