2

Why is the postfix increment operator (++) executed after the assignment (=) operator in the following example? According to the precedence/priority lists for operators ++ has higher priority than = and should therefore be executed first.

int a,b;
b = 2;
a = b++;
printf("%d\n",a);

will output a = 2.

PS: I know the difference between ++b and b++ in principle, but just looking at the operator priorities these precende list tells us something different, namely that ++ should be executed before =

Jürgen Brauer
  • 423
  • 3
  • 6

4 Answers4

7

++ is evaluated first. It is post-increment, meaning it evaluates to the value stored and then increments. Any operator on the right side of an assignment expression (except for the comma operator) is evaluated before the assignment itself.

owacoder
  • 4,815
  • 20
  • 47
  • Plus one for mentioning the comma operator, which separates expressions. – Bathsheba Oct 19 '15 at 13:45
  • Ok. I think, I understand now. E.g. in a = b++ * c++ * d++ you mean that the ++ operator first gives use the current value of b, c, and d, then the two * operators are processed, the result is assigned to a (assignment operator =) and then the variables of b,c, and d are incremented. Right? – Jürgen Brauer Oct 20 '15 at 21:06
  • 1
    @JürgenBrauer - Actually, I don't think it is defined by the standard *when* in evaluation the increment/decrement is performed, except for "before the next sequence point." It could be performed before or after the assignment, it would just have to be performed before anything after the semicolon, the next sequence point. See [this question](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) for more information. Although the question is tagged C++, the same applies (generally) to C. – owacoder Oct 21 '15 at 04:07
3

It is. It's just that, conceptually at least, ++ happens after the entire expression a = b++ (which is an expression with value a) is evaluated.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

Operator precedence and order of evaluation of operands are rather advanced topics in C, because there exists many operators that have their own special cases specified.

Postfix ++ is one such special case, specified by the standard in the following manner (6.5.2.4):

The value computation of the result is sequenced before the side effect of updating the stored value of the operand.

It means that the compiler will translate the line a = b++; into something like this:

  • Read the value of b into a CPU register. ("value computation of the result")
  • Increase b. ("updating the stored value")
  • Store the CPU register value in a.

This is what makes postfix ++ different from prefix ++.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

The increment operators do two things: add +1 to a number and return a value. The difference between post-increment and pre-increment is the order of these two steps. So the increment actually is executed first and the assignment later in any case.

Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
  • Sorry for my over-simplication. Of course I meant that the expression is evaluated, but the concept of operators or functions returning something sounds much simpler to beginners. – Hubert Grzeskowiak Oct 19 '15 at 13:45
  • @iharob An expression is any legal combination of symbols that represents a value. The increment operators are, as their name say, operators. Operators don't represent any value, thus they are not expressions. Please go read the difference between them. – Daniel Muñoz Parsapoormoghadam Oct 19 '15 at 13:59
  • @DanielMuñozParsapoormoghadam You are right, I meant `a++` is an expression not the operator. It wasn't clear as I said it I think. – Iharob Al Asimi Oct 19 '15 at 14:01