Prefix and postfix operator

56 Views Asked by At
#include <stdio.h>

int main()
{
   int x=5, y;
    y=x+++x;
    printf("%d", x);
    printf("%d", y);
}

What I found is that postfix increment has higher precedence than prefix. Hence y=x+++x; y=(x++)+x; y=10 x=6 But when I execute the program :y=11,x=6 Please correct me if I am understanding anything wrong

1

There are 1 best solutions below

1
On

Splitting it down and remembering that there is also the "right-left rule" This is a simple rule that allows you to interpret any declaration. It runs as follows:

Start reading the declaration from the innermost parentheses, go right, and then go left.

So, parsing Right-to-Left in the absence of parentheses:

x=5, so using the prefix operator ++x increments the value before y is assigned.

Then an assignment is made to y equivalent to 5+6 (i.e. x+(++x))

x was incremented, so x=6