-4

I am facing problems in understanding the increment orders in C++.

I am aware that increments are unary operators thus they come after parenthesis from right to left.

My question is when do we increase the number?

Here is a simple code:

#include <iostream>
using namespace std;

int main()
{
  int a1;
  int a(12),b(3);

  a1=7+10%3-5;
  b=a/b++;

  cout<<a1<<"\t"<<b<<endl;
  return 0; 
}

Here I get a=3 that's right but b=5 , I think it is 3 because we start from the right and increase by 1 then 12/4 gives 3.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
aysh_99
  • 11
  • 3
  • 4
    `b++` increments _after_ getting `b`'s value. `++b` would increment _before_ getting `b`'s value. – Uwe Keim Mar 21 '19 at 15:45
  • 1
    @UweKeim I think K&R said `b++` increments `b` after the calculation is done, not after getting `b`'s value. – Aykhan Hagverdili Mar 21 '19 at 15:53
  • 1
    Thanks for clarifying, @Ayxan . – Uwe Keim Mar 21 '19 at 15:55
  • 1
    @UweKeim I rechecked the book and it says "But the expression `++n` increments `n` *before* its value is used, while `n++` increments `n` *after* its values has been used." That means I was wrong. Sorry for the confusion – Aykhan Hagverdili Mar 21 '19 at 16:09
  • @Ayxan: If the book assigns that to `n` then eat it. – Bathsheba Mar 21 '19 at 16:26
  • @Bathsheba it doesn't assign anything to itself. It's [K&R](https://en.wikipedia.org/wiki/The_C_Programming_Language) we are talking about here :D The book was just describing what happens when you do `x = n++;` – Aykhan Hagverdili Mar 21 '19 at 16:32
  • @Ayxan: Which is well-defined: `x` gets the unincremented `n`. – Bathsheba Mar 21 '19 at 16:33
  • hey, thanks for your replies. But what I really wanted to know was when exactly the increment happens. Does it divide 12 by 3 .... get the answer and adds 1 to it? I understood that it adds 1 to b .... divide 12 by 4 and you get 3 !! please correct me if I am missing something. – aysh_99 Mar 21 '19 at 16:33
  • @Bathsheba what happens when I do `x = 2 * 5 + n++`? Does `n` get incremented immediately after `n++` is evaluated and its value is taken, or does it wait until the end of the calculation (statement)? – Aykhan Hagverdili Mar 21 '19 at 16:36
  • 1
    `n++` is the original value of `n` for the evaluation of the expression. I would avoid saying *statement* since `x = 2 * 5 + (n++, n)` recovers the pre-increment behaviour because in this guise `n++` *is* the expression. – Bathsheba Mar 21 '19 at 16:36
  • @Bathsheba that's what I was looking for. What do you mean by "recovers"? – Aykhan Hagverdili Mar 21 '19 at 16:39
  • @Ayxan: "Is the same as". Sorry; flowery English. – Bathsheba Mar 21 '19 at 16:40

1 Answers1

4

Note that the C++ grammar implies that the associativity of the postfix increment is from left to right, and that of the prefix increment is right to left.

The behaviour of b = a / b++; is actually undefined. This is because = is not a sequencing point, so there are simultaneous reads and writes on b.

(The same applies to C.)

It's a variant on i = i++;: for more on that see Is the behaviour of i = i++ really undefined?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483