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

Possible duplicate:
What is the difference between ++ me and me ++
pre Decrement and post Decrement

Yes, I'm a nob, but I completely forgot what they both do.

I know, however, that int ++ just adds the value to the int.

So what is ++ int?

Thank.

+3


source to share


4 answers


If you are talking about C (or C) languages, it is exactly the same if you don't use value:

int a = 10;
int b = a++;

      

In this case, a

it becomes 11, and b

- 10. This post-increment - you increase it after use.

If you change this line above to:



int b = ++a;

      

it a

still becomes 11, but also b

. This is because it pre-magnifies - you magnify before use.

Note that this is not exactly the same for C ++ classes, there is efficiency to be gained by preferring one over the other. But since you're talking about integers, C ++ acts the same way as C.

+10


source


a ++ will return a and increment it, ++ a will increment a and return it:

a = 5; b = a++; // b = 5, a = 6



a = 5; b = ++a; // b = 6, a = 6

+3


source


Every expression in C or C ++ has a type, value, and possible side effects.

int i;
++i;

      

Type ++i

- int

. An increase is a side effect i

. The expression value is the new value i

.

int i;
i++;

      

The type i++

is int

. An increase is a side effect i

. The expression value is the old value i

.

+3


source


this is the preincrement operator

good explanation here

+1


source







All Articles