answersLogoWhite

0

Difference between plus plus p and p plus plus?

Updated: 8/19/2019
User Avatar

Wiki User

12y ago

Best Answer

++p increments p by 1 unit and returns the result. This is known as pre-increment.

int p = 0;

int q = ++p; // q=1, p=1.

This is effectively the same as saying:

int p = 0;

p = p + 1; // p= 1.

int q = p; // q = 1.

p++ also increments p by 1 unit, but returns the previous value of p, not the current value. This is known as post-increment.

int p = 0;

int q = p++; // q=0, p=1.

This is the same as saying:

int p = 0;

int q = p; // q = 0.

p = p + 1; // p = 1.

Of the two forms, ++p is marginally quicker because p++ employs a temporary variable for the return value, whereas ++p does not. As such, ++p is the preferred form for looping purposes:

for( int p=0; p<100; ++p);

Which version you use depends upon which value you want returned from the operator, whether it is the previous value or the new value.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between plus plus p and p plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp