In your first example , you are comparing *(pointer dereference)
with postfix/prefix
operators.
*(pointer dereference)
in this case, has equal precedence with ++(prefix)
but lower precedence than ++(postfix)
. Also note that prefix ++ and * have Right to Left associativity and ++ postfix has left to right.
Now see *ptr++
, ++ is postfix so first postfix is evaluated than *, hence its *(ptr++)
.
Now see ++*ptr
, ++ is prefix, so equal precedence, so associativity will come into picture, and * will be evaluated first (because of right to left nature) and then ++ hence its ++(*ptr)
.
Now in your second example,
b=a++
-> a++ means that postfix on a
, so value is assigned first and then incremented, But this is the property of postfix, so a's
value will go into b
and then a
will be incremented.
c=++a
-> ++a means that prefix on a, so first a
is incremented and then value of a
goes into c
.
This example only shows the property of both the operators. Their functionality is performed on two different operations. To compare their precedence, you have to take an example where both are operated into a single statement.
The example can be
int a=5,b;
b= a+++a;
now here the expression will be a++ + a, not a + ++a, because the precedence of postfix is higher than prefix.
++
and prefix++
are in different expressions. – Speechmaking(b=a)++
orb=(a++)
. – Leigha