4

I get very confused at times with the shorthand increment operation.

Since when i was little programming in BASIC, i got some how stuck with a = a+1 which is long painful way of saying 'Get a's current value, add 1 to it and then store the new value back to a'.

1] a = a +1 ; 

2] a++ ;

3] ++a;

4] a +=1;

[1] and [4] are similar in functionality different in notation, right?

2] and 3] work simply differently because of the fact that the increment signs ++ is before and after. Right?

Am I safe to assume the below?

int f(int x){ return x * x;}

y = f(x++) -> for x =2, f(x) = x^2

f(x)     ======> y= 2^2 =4 

x=x+1;   ======> x= 2+1 = 3 



y = f(++x) -> for x =2, f(x) = x^2

x=x+1    ===========> x = 2+1 = 3

f(x)     ===========> y =3^2 = 9
Sina Iravanian
  • 16,011
  • 4
  • 34
  • 45
Athapali
  • 1,091
  • 4
  • 25
  • 48
  • I don't knwo and I am interested in knowing. I have a feeling that can lead to undefined behaviour. `f(x++)` when does the increment take place? After passing the value and then immediately incrementing or running the function and den increment at the end? – Suvarna Pattayil Apr 12 '13 at 14:22
  • @SuvP: It's actually easier to understand if you consider operator overloading, i.e. when you are writing the implementation of those operators yourself: Both prefix and postfix increment **do** reliably increment their operand, and both **do** reliably return a well-defined value: Prefix the **new** value of the operant, postfix the **old** one. For non-trivial operands, this makes prefix the *potentially* faster operation, because there might (!) be a temporary object involved. In no case will you end up with undefined behaviour, as long as you don't daisy-chain them: `x = x++ + x++`... – DevSolar Apr 12 '13 at 14:25
  • 1
    @SuvP: The C# language guarantees that the side effect of the increment takes place *before* the invocation of the function, provided that the side effect is observed from the same thread. The C and C++ languages do not; this is implementation-defined behaviour in those languages. – Eric Lippert Apr 12 '13 at 14:50
  • 2
    @DevSolar: In C#, crazy expressions like `x = x++ + x++` are well-defined by the specification. In C and C++, that is implementation-defined behaviour. – Eric Lippert Apr 12 '13 at 14:50
  • 1
    @EricLippert: I actually have **zip** experience with C#, and was extrapolating from C/C++. Thanks for clarifying. **However**, the call `f( x++ )` **is** well-defined in C and C++ as well, as the parameter *is* evaluated *before* `f()` is called. What is *not* well-defined is the *sequence* in which parameters are evaluated (e.g. `f( x++, x )`). – DevSolar Apr 12 '13 at 14:57
  • 3
    @SuvP: Whoops, DevSolar is correct. C and C++ do require that the side effects be observed *before* the function is entered, just as C# does. I was misremembering; as DevSolar correctly states, C/C++ do *not* state what order side effects are observed in for expressions like `f(x++, x)` - we know that x will be incremented before the function call, but not whether the left argument's side effect will happen before or after the right argument is evaluated. (In C#, argument side effects occur left-to-right always.) – Eric Lippert Apr 12 '13 at 15:02

3 Answers3

20

Difference is, what the operator returns:

The post-increment operator "a plus plus" adds one, and returns the old value:

int a = 1;
int b = a++;
// now a is 2, b is 1

The pre-increment operator "plus plus a" adds one, and returns the new value:

    a = 1;
    b = ++a;
// now a is 2 and b is 2
metadings
  • 3,798
  • 2
  • 28
  • 37
  • In `f(x++)` when does the increment take place? After passing the value and then immediately incrementing it and using in the function or running the function and den incrementing it at the end? I mean is this behaviour (whatever it might be always assured? or is it undefined) – Suvarna Pattayil Apr 12 '13 at 14:23
  • @SuvP The increment happens before the expression evaluates and returns. In this case `x++` will return the _old_ value of `x` to `f()`; before `f` begins its execution the _field_ (or local var `x`) will increment. EDIT: Here's an equivalent-code breakdown of the `++` which might help you track its logical flow: http://stackoverflow.com/questions/14175964/why-dont-a-a-operation-increase-the-value-a/14175978#14175978 – Chris Sinclair Apr 12 '13 at 14:25
  • 1
    @SuvP independent of what your `f` does, `x++` is post incrementing the `x`, passing the old value of `x` to `f` – metadings Apr 12 '13 at 14:26
  • Okay, so the passed value is copied into the local variable and then that is incremented before rest of the execution takes place. – Suvarna Pattayil Apr 12 '13 at 14:30
  • @SuvP The operator saves the old value, increments the variable's value and returns the old value. Then f will be executed. – metadings Apr 12 '13 at 14:36
7

First off, you should read this answer very carefully:

What is the difference between i++ and ++i?

And read this blog post very carefully:

http://blogs.msdn.com/b/ericlippert/archive/2011/03/29/compound-assignment-part-one.aspx

Note that part two of that post was an April Fool's joke, so don't believe anything it says. Part one is serious.

Those should answer your questions.

When you have just an ordinary local variable, the statements x++; ++x; x = x + 1; x += 1; are all basically the same thing. But as soon as you stray from ordinary local variables, things get more complicated. Those operations have subtleties to them.

Community
  • 1
  • 1
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
1

1], 3] and 4] are functionally identical - a is incremented by one, and the value of the whole expression is the new value of a.

2] is different from the others. It also increments a, but the value of the expression is the previous value of a.

DevSolar
  • 67,862
  • 21
  • 134
  • 209