Difference between pre- and postfix incrementation in C (++a and a++) [duplicate]

Remember, C and C++ are somewhat expressive languages.

That means most expressions return a value. If you don’t do anything with that value, it’s lost to the sands of time.

The expression

(a++)

will return a‘s former value. As mentioned before, if its return value is not used right then and there, then it’s the same as

(++a)

which returns the new value.

printf("%d\n", a++); // a's former value
printf("%d\n", ++b); // b's new value

The above statements will work as you expect, since you’re using the expressions right there.

The below would also work.

int c = a++;
int d = ++b;

printf("%d\n", c); // a's former value
printf("%d\n", d); // b's new value

Browse More Popular Posts

Leave a Comment