Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Postfix operator really has a higher precedence than prefix? [closed]

However It is clearly written in precedence table that postfix operator has higher priority than prefix. But still I have a daubt.
I start with following example:

*ptr++;   // evaluate as *(ptr++);
++*ptr;   // evaluate as ++(*ptr);

This proves that postfix operator has higher precedence than pre one.

Now in following example it doesn't seems true:

int a=0,b,c;
b=a++;   //b=0 ,here it seems ++ has lower priority that is after assignment increment is performed.
c=++a;   //b=2 ,here it seems ++ has higher priority.

In above example, Doesn't it seems postfix operator has lower priority than prefix?

like image 766
A.s. Bhullar Avatar asked Jul 04 '14 17:07

A.s. Bhullar


People also ask

Which has more precedence postfix or prefix?

Postfix increment and decrement has higher precedence than prefix increment and decrement. The operand must have integral, floating, or pointer type and must be a modifiable l-value expression (an expression without the const attribute). The result is an l-value.

Which operator is having the highest precedence postfix?

In C/C++, precedence of Prefix ++ (or Prefix –) has same priority than dereference (*) operator, and precedence of Postfix ++ (or Postfix –) is higher than both Prefix ++ and *.

Which operator has highest order of precedence?

The logical-AND operator ( && ) has higher precedence than the logical-OR operator ( || ), so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- .

Which operator is having highest precedence is postfix and lowest is equality?

Explanation: Because the * operator is having highest precedence, So it is executed first and then the + operator will be executed.


1 Answers

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.

like image 148
Mrigank Avatar answered Sep 19 '22 08:09

Mrigank