# Thread: difference between ++ and +=1

1. ## difference between ++ and +=1

Hi all,
Can any one tell the difference between the following.

a++
a+=1

Both will do the same operation.I want to know that any one of these have a advantage than the other.

2. While ++a and a++ mean the same thing when they form statements independently, they behave differently when they are used in expressions on the RHS of an assignment statement.
For eg.
a=5;
y=++a;
In this case the value of y would be 6. But in this case
a=5;
y=a++;
here y would be 5 but a would be 6
On the other hand a+=1 is shorthand form of a=a+1.

3. Thank you BEN10.But already I know what you explained.I want to know whether both of these statement have some difference in the case of speed or not.

4. They are two different kinds of operators. ++ is a unary operator (whether x++ or ++x). += is an assignment operator. I doubt there is much difference in speed but I don't know. The potential use value is slightly different:
Code:
`x += y++;`

5. I'd say that it depends on compiler optimisations.

I'm guessing that ++x would be translated by most compilers into the assembly 'INC' (increment) instruction mnemonic, which may or may not be faster than 'ADD'.

If a compiler optimises any code with a 'x + 1' to the increment instruction then there would be no difference.

Same goes for decrementing.

Note that this is just speculation. I don't know if any of this is what actually happens.

6. Assignment operators evaluate the l-value only once, which is an important thing to watch out for if it contains any side effects.

Eg.
array[i++] += 1;
will do the right thing.

Either of these attempts leads to all sorts of trouble.
array[i++] = array[i] + 1;
array[i++] = array[i++] + 1;

Doing it correctly the long way is hard work.
array[i] = array[i] + 1;
i = i + 1;

There would certainly be a big performance difference if you had
array[someFuncReturningIndex()] += 1;
because the function would only be called once.

7. @Salem: good use of fonts

8. Just an FYI,
I read about the origin of "C", and how the compiler favored the reigning hardware platform of that time - PDP family of minis. So the post-increments and similar had a reason for being part of the language. There were native machine instructions of that ilk: access-then-increment. I haven't encountered any micro processors which had similar instructions but then again, the desired effect can be achieved with two instructions at most.

9. Originally Posted by nonoob
Just an FYI,
I read about the origin of "C", and how the compiler favored the reigning hardware platform of that time - PDP family of minis. So the post-increments and similar had a reason for being part of the language. There were native machine instructions of that ilk: access-then-increment. I haven't encountered any micro processors which had similar instructions but then again, the desired effect can be achieved with two instructions at most.
Both VAX (32-bit version of PDP-11 really) and 68000 have post-increment and pre-decrement versions of it's instruction set. I'm fairly sure that ARM's instructions also matches the C language fairly well with it's STR/LDR instructions when setting the P and W bits to one of the instruction.

--
Mats

10. Thank you! I stand corrected. I forgot about Motorolas. Never could wrap my head around 6800, 68000 addressing, registers, but probably the assembly syntax turned me off most of all. I'm a Zilog/Intel guy myself.

I'd say that it depends on compiler optimisations.

I'm guessing that ++x would be translated by most compilers into the assembly 'INC' (increment) instruction mnemonic, which may or may not be faster than 'ADD'.

If a compiler optimises any code with a 'x + 1' to the increment instruction then there would be no difference.

Same goes for decrementing.

Note that this is just speculation. I don't know if any of this is what actually happens.
x++ doesn't mean that the increment value is 1, it can be anything, so the use of something like "INC" is a very special case.

Stephane.

12. x++ doesn't mean that the increment value is 1, it can be anything
It doesn't? How do you set it to increment by something else?

13. >> x++ doesn't mean that the increment value is 1, it can be anything, so the use of something like "INC" is a very special case.

Well, the only other case is in pointer arithmetic, where the increment means "advance sizeof(type) bytes from this offset". Besides that, though, I can't think of any other possible forms.

14. Back to the original question... even in the case of pointer arithmetic a++ and a+=1 would still be equivalent. If a compiler generates markedly different machine code which varies in execution times, then shame on it!

15. Yes Sebastiani, I was referring to pointer arithmetic which I think it is important to mention, since the ++ operator comes from the PDP 11 autoincrement mode : (Rn)+ (see Department of Computer Science - PDP-11 - Don S. Bidulock).

Often the question asked is "which is faster, x++ or x += 1 ?". In that case it's important to remember that "1" can be any other value (since an incrementer != adder).