ElConrado
ElConrado

Reputation: 1640

Meaning of single + prefix in incrementation

I download a code which provide integer value incrementation by 5 like this:

int i =0;
i = +i +5;

I usually use:

i+=5;

Is there any difference beetwen this two form? Is it special meaning od single prefix plus?

Upvotes: 0

Views: 82

Answers (2)

Boris Uskov
Boris Uskov

Reputation: 107

In most cases i = +i +5; / i=i+5 is equivalent to the i = i +5;

But in the i+=5, i is only evaluated once (see += operator). It may take an effect in the case of volatile variable, because of side effect.

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 157068

If you ask if there is any difference between i = +i +5; and i = i + 5;, no, there is not.

+5 is just the same as 5, and it is the 'opposite' of -5. The + sign is just allowed for consistency here (it would be 'weird' to be able to use the minus sign to sign a number, but not the plus sign).

Upvotes: 3

Related Questions