I have a question about C Operator Precedence and Associativity. Newbie. Help me

62 Views Asked by At

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:
  1. First precedence has "i++", it must be evaluated at first;
  2. Second precedence has "(i *= 2)";
  3. 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:

  1. (i *= 2), return 20 // i = 20
  2. i++, return 20 // i = 21
    21 + 21 + 20 + 20
0

There are 0 best solutions below