Datesfat Chicks
5/21/2011 2:08:00 AM
On Fri, 20 May 2011 14:53:01 -0700 (PDT), Chad <cdalten@gmail.com>
wrote:
>Say I have something like...
>
>int sum = 0;
>int i = 3;
>
>sum = sum + i;
>
>Would 'sum + i' refer to the value 3? Or does 'sum' just get replaced
>with 0 and 'i' with 3 before it gets evaluted as 0 + 3.
Hi Chad,
The initial declarations
>int sum = 0;
>int i = 3;
are a shorthand way in C with automatic variables to declare the
variables and assign them at the same time. These aren't
constants--they are variables.
In most environments, the compiler will allocate the stack space for
the variables, then assign them, the same as if one had written
int sum;
int i;
sum = 0;
i = 3;
In the statement
>sum = sum + i;
"+" has a higher priority than "=" (by design), so the compiler will
retrieve sum, retrieve i, add them, and store the result back in sum.
As other posters have pointed out, with an optimizing compiler, all
bets are off. What the compiler does will depend on what it
determines about whether and how sum and i are otherwise used.
DFC