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

2

7

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?

Bobbitt answered 4/7, 2014 at 17:53 Comment(5)
In your second example postfix ++ and prefix ++ are in different expressions.Speechmaking
Precedence has nothing to do with execution order. Precedence controls grouping. Precedence is the difference between parsing an expression like (b=a)++ or b=(a++).Leigha
I don't think your first example does show that postfix operators have higher precedence that prefix. The first line shows that the postfix operator has higher precedence than the * (pointer dereference). The second line doesn't really show anything - the compiler can't interpret this statement any other way.Gage
For language references, please refer to either the official C standard, or a compiler authority such as the Gnu C Reference Manual - Operator Precedence. It is not uncommon for random internet documentation to be just plain wrong.Bondmaid
Thank you, I've always wanted to ask the same question since a long time. It's a great question! And now thanks to you, I've got a much more clear understanding of it. And I just don't understand why some people should mark this as "unclear", for me, your question is so clear. I hate these guys.Kristofer
F
15

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.

Fenugreek answered 4/7, 2014 at 18:51 Comment(0)
O
2

The example in question only appears wrong, due to misunderstanding by questioner.

Because i++ is return value of i THEN POST-increment, ++i is increment i and then return the value.

Also *p++ is designed so that on a PDP11, strcpy could be written as while (*cd++ = *cs++); That copies str, cs to cd, until the terminating NULL when value *cd == 0.

Precedence, you look up in a table, if your compiler doesn't follow the C standard, submit a bug report. In general mixing too many side effects like the suggest c = ++*p++ expression is just asking for trouble, you're pre-incrementing *p, and post-decrementing p, which was NOT what you expected. The operations are applied on different memory locations, so tell you nothing about operator precedence.

An expression using parentheses like (++*bar)++; won't compile, because (++*bar) is not an l-value, which disproves the hypothesis in the question, quite simply :

bar = foo;  ++*(bar++); puts( foo); putchar( '\n');
bar = foo;  (++*bar)++; puts( foo); putchar( '\n');

Produces error on 2nd line :

prog.c:9:22: error: lvalue required as increment operand
bar = foo; (++*bar)++; puts( foo3); putchar( '\n');
                   ^

Not the first, showing that ++ has precendence.

Orotund answered 4/7, 2014 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.