back to home page - back to programming index
AC_C Site - i++; , ++i, i += N and i = i + N; Overview from C/C++ and Assembler Perspective;

Note that Examples are done using UNIX terminal and using GCC Compiler

- Compare between i++ and ++i (Prefix inrement vs Post Fix Increment)
Basic Differance C Level\Enviorment Lets review the Basic Example (Example 1)

at first it looks like the operators are identical however we will review this in detail.


DIfferance 1 - Operations increment at different times.


Differance 1 (Example 2) - the Postfix increment now showd in the printf()

The reason is that postfix Increment happen after assignment performed,
its important to pay attention to this as it can easily misslead

DIfferance 1 (Example 3) - that posfix increment may mislead.

What will be i value in line 10? the operator will actually not do anything...
the ++ increment the RValue after it been assign (with =) to i from Left (LValue) so the increment done the RValue that not in use anymore and therefore vanished.

DIfferance 1 (Example 4) - that posfix not applay from function (by Value)

Note: because i passed by value the original 'i' from main() is not expected to be incremented..

DIfferance 1 (Example 5) - that posfix not applay from function (by refferance)

Note: because i passed by refferance the i is expected to be incremented, but because of postfix inrement it will not return from incPost

Conclution 1 - it more safe to use ++i, it prevent from unexpected behaive that developer not intended.



Differance in ASM Level

Differance 2 - Operation work Differantly at Instruction Level (ASM).


Lets review the Basic Example again (Example 1)

now lets recompile it again to see the ASM commands.
Notes
1. we turn off the any optimization of the compiller to get accurate code as much as possible.
2. we turn off printing of unwind tables.
3, we use command igcc -S example1.c -fno-asynchronous-unwind-tables -o0


Observe that between the printf the ASM code for prefix inrement and postfix increment

the code look identical...

in this section we will discuss about the differances in ASM level.