RE: Handling pre-increment and post-increment in GCC

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Manjunath B S wrote:

> return printf("Post increment : %d, Pre increment : %d\n", i++, ++i);

You should avoid this sort of thing if possible. The C standard makes no
guarantees about the order of evaluation in cases like this; this is
undefined behaviour.

In practice, how this is evaluated will depend on each architecture's ABI.
i686 ABIs, for example, do not generally pass function arguments in
registers but instead on the stack: in order for a varargs function like
printf() to find its arguments in the correct order they must be placed on
the stack from right to left and so are generally evaluated that way. (That
said, I don't understand what you're seeing: I would expect ++i to be
evaluated before i++ and so you'd see 3,3. However your optimisation options
may change this.) I don't know the PowerPC ABI but as the PowerPC has many
more registers I expect that some arguments will be passed in registers even
for varargs functions: in that case it doesn't matter which order the
compiler evaluates i++ and ++i in since it can store them in any order, so
as you can see this is evaluated left-to-right - but if you add many more
arguments to your printf then you may see a different evaluation order for
later arguments that do get passed on the stack.

So in summary: you'd do better to avoid this rather than to try and
understand this and fight against it!

Rup.


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux