0

Case 1:

int i = 10;
int j = i++;

Here, first the value of i is assigned to j and than i is increased by 1.

Case 2:

int i = 10;
int j = ++i;

Here, first i is increased by 1 and than it is assigned to j.

So, If the operation of increment is done first in the prefixed form, then why postfixed has higher precedence than prefixed?

msc
  • 33,420
  • 29
  • 119
  • 214
  • The precedence of an operation is loosely related to the nature of the operation itself. – Eugene Sh. Dec 08 '17 at 19:36
  • prefixed = increment and return, postfixed return and increment – geckos Dec 08 '17 at 19:39
  • 1
    Um.. not sure why *that* duplicate... – Eugene Sh. Dec 08 '17 at 19:47
  • @EugeneSh. - I can change it to a better one if you know of one :) – Oliver Charlesworth Dec 08 '17 at 19:56
  • Precedence is an independent concept of evaluation order. Evaluation order is constrained by dependencies: an operation requiring its input operands to be evaluated before it can produce output. Given `a * (b + c)`, we can evaluate `a`, `b` and `c` before performing a single arithmetic operation, and the same holds true of `a * b + c`. – Kaz Dec 08 '17 at 20:09
  • All precedence does is group operators with operands - it does not affect the order in which operations are evaluated. For example, *precedence* dictates that `++b[i]` is *parsed* as `++(b[i])` - it has nothing to do with the order in which the subexpressions are evaluated. – John Bode Dec 08 '17 at 20:35

2 Answers2

2

This has nothing to do with precedence.(here) In pre-increment the value that is assigned is the value before the side effect takes place. For pre increment it will be the value after the side effect.

int i = 10;
int j = i++;

Before incrementing value of i is 10. So j=10 after these 2 statements are executed.

int i = 10;
int j = ++i;

Here the value will be 11. because incremenet is done first and then it is assigned.

user2736738
  • 30,591
  • 5
  • 42
  • 56
0

Linux manual page of operator define as same priority for both pre/post increment.

! ~ ++ -- + - (type) * & sizeof      right to left

post increment rule is first assigned and then increment

int j = i++;// here first whatever i value is there that will be assigned to j

Pre increment rule is first increment and then assign

int j = ++i;//++i itself will change i value & then modfied value will assign to j.

for e.g consider below example

#include<stdio.h>
int main()
{
        int x = 10;
        int *p = &x;// assume p holds 0x100
        printf("x = %d *p = %d p = %p\n",x,*p,p);
        ++*p++;
        /** in above statement ++ and * having same precedence,then you should check assocaitivity which is R to L
          So start solving from Right to Left, whichever operator came first , solve that one first 
          first *p++ came , again solve p++ first, which is post increment,so address will be same 
         *0x100  means = 10
         now ++10 means = 11
         **/
        printf("x = %d *p = %d p = %p\n",x,*p,p);
}
Achal
  • 11,821
  • 2
  • 15
  • 37
  • that's incorrect. Here is the correct precedence table: http://en.cppreference.com/w/c/language/operator_precedence – Eugene Sh. Dec 08 '17 at 19:45
  • yes true but what's the difference b/w both ? what ever operator I mentioned in one line all are having same priority or priority 1 according to link provided by you. Associativity is right to left, start from right to left, what comes first that having highest priority among all in precedence 1. – Achal Dec 08 '17 at 19:49
  • 1
    The difference is when you do something like `++*p++`. – Eugene Sh. Dec 08 '17 at 19:51
  • I have no doubt in that. Both * and ++ having same priority So starts from Right to left, so first *p++ will be performed. – Achal Dec 08 '17 at 19:55
  • I see what you mean. Will try to think of a counter-example (if there is any :) ) – Eugene Sh. Dec 08 '17 at 20:03
  • Sorry boss My intention is correct only, in hurry I wrote priority in comments it should be precedence. – Achal Dec 08 '17 at 20:05