2
int main()
{
  int a=10;
  int *b=&a;
  int c=*b++;
  printf("%d",c);
}

I know following program outputs 10.but according to precedence table which gives precedence of operator http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm) ,post-fix ++ has higher precedence than = and *.so ++ should evaluate first and then *.then why program is printing output as 10?

Steve Fallows
  • 6,274
  • 5
  • 47
  • 67
user3526905
  • 171
  • 1
  • 12
  • Maybe you meant to try this: `int c = (*b)++;` (prints 10) versus `int c = ++(*b);` (prints 11). In both cases, `a` becomes 11. – JS1 Nov 04 '14 at 20:44

5 Answers5

3

It does have higher precedence, which means the pointer will get increased by one, not the value it points to.

But the increase by one is sequenced to the end of the evaluation( that is what postfix ++ does, and that is not directly related to precedence ), so the value you get out of the pointer is the old one: p not p+1. Having that pointer, you dereference it and get the value of a, which is 10. Then the pointer p is incremented by one.

int c = *p ;
p++ ;
2501
  • 25,460
  • 4
  • 47
  • 87
0

++ has higher precedence than * means operand b will bind to ++ first as

 int c = *(b++);

It doesn't mean in any way that evaluate b++ first and then dereference the evaluated value. In *b++, ++ will have the same effect on b as that post increment operator do. *b++ is simply: Dereference original value of b and increment the pointer b by 1.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

As you say, ++ has higher precedence than *. So int c=*b++; parses as int c=*(b++);. However, the result of the post-increment operator is the value before incrementing. In other words, the assignment is equivalent to

int temp = b;
b = b + 1;
c = *temp;
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Assignment may also equivalent to `int temp = b; c = *temp; b = b + 1;`. – haccks Nov 04 '14 at 18:52
  • @haccks Certainly. I think the way I wrote it is clearer in the current context since the first two lines are equivalent to the post-increment. The lack of proximity in your version blurs this issue. – Code-Apprentice Nov 04 '14 at 18:58
0

The ++ will do an increment of b, a memory address, after evaluation in the context of the larger expression.

From http://msdn.microsoft.com/en-us/library/e1e3921c.aspx:

It is important to note that a postfix increment or decrement expression evaluates to the value of the expression prior to application of the respective operator. The increment or decrement operation occurs after the operand is evaluated. This issue arises only when the postfix increment or decrement operation occurs in the context of a larger expression.

So, what happens is that you apply a post-fix increment to b, but the dereference * is given the original value of b, which points to 10. If you were to print out b or *b you'll see that the value and address have changed to something unexpected.

wilkesybear
  • 528
  • 3
  • 9
0

Yes, ++ has higher precedence than *. So, the statement

int c=*b++;

will be evaluated as

int c=*(b++)

Since it's post-fix operator, the pointer 'b' is incremented first, but it returns the old address of 'b'(which points to address storing the value 10 ). Therefore, c will have the value 10.

Glad Born
  • 43
  • 5