[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

A question about variables...

Chad

5/20/2011 9:53:00 PM

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.


Chad
7 Answers

Shao Miller

5/20/2011 10:18:00 PM

0

On 5/20/2011 17:53, Chad 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.

On the last line, 'sum' will evaluate to the last-stored value '0' and
'i' will evaluate to the last-stored value '3' and those two will be
added together to result in '3' and that value will be stored to 'sum'.
And after that, the 'sum = ...' expression evaluates to the value that
was stored: '3'. After all that, that last result goes unused.

sum = sum + i;
sum = 0 + 3;
sum = 3;
3;

Seebs

5/20/2011 10:21:00 PM

0

On 2011-05-20, 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.

I don't think I understand the distinction you're making. In the abstract
semantics of the machine, what happens is:
retrieve value from sum (0)
retrieve value from i (3)
add two retrieved values together (yielding 3)
store this new value (3) in sum

In practice, a good optimizer might well just set sum to 3 in the first
place because it knows what happens.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

China Blue Veins

5/20/2011 11:05:00 PM

0

In article <c2ab820a-674b-4bfe-a636-e6abeb10fcba@r33g2000prh.googlegroups.com>,
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.

You can apply the principle of quantum indeterminancy: if there is no observable
difference amongst the various states, all of the states are equally valid.

Some compilers only assign to a real variable once so the above becomes
int _0 = 0;
int _1 = 3;
int _2 = _0 + _1;
and source strings like sum and i are merely labels to current underlying
variable it represents.

--
I remember finding out about you, | Accept no substitutions.
Everyday my mind is all around you, | I'm whoever you want me to be.
Looking out from my lonely room | Annoying Usenet one post at a time.
Day after day. | At least I can stay in character.

Datesfat Chicks

5/21/2011 2:08:00 AM

0

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

James Kuyper

5/21/2011 2:14:00 AM

0

On 05/20/2011 05:53 PM, Chad 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.

Your question is very odd. My best guess is that it reflects a serious
misunderstanding of the relationship between the source code and the
resulting program. But that doesn't make must sense, either - as I
recall, you've shown a considerably higher level of understanding in
previous messages posted to this newsgroup; at least, they were posted
using the same e-mail address.

The line in the source code which says

sum = sum + i;

Tells the compiler to create a program containing machine instructions
for the computer which tell it to perform the following tasks:

1. Retrieve the value currently stored in the variable named 'sum'.
2. Retrieve the value currently stored in the variable named 'i'.
3. Calculate the total of those two values.
4. Store the result into the variable named 'sum'.

Typically, the instructions will not refer to the variables by name.
Each of those variables are assigned to a different location in memory,
and the generated instructions simply refer to that memory location. All
trace of the character strings "sum" and "i" have disappeared by the
time the program has been created (that's not quite true, if the
variables have external linkage or if you compile in debugging mode -
but it's accurate enough for the purposes of this explanation).

Therefore, "0" does not replace "0", because the program doesn't contain
"sum", it contains machine instructions.
--
James Kuyper

James Kuyper

5/21/2011 12:05:00 PM

0

On 05/20/2011 10:14 PM, James Kuyper wrote:
....
> Therefore, "0" does not replace "0", because the program doesn't contain

Correction:
.... "0" does not replace "sum" ...

--
James Kuyper

Chad

5/21/2011 5:43:00 PM

0

On May 20, 7:14 pm, James Kuyper <jameskuy...@verizon.net> wrote:
> On 05/20/2011 05:53 PM, Chad 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.
>
> Your question is very odd. My best guess is that it reflects a serious
> misunderstanding of the relationship between the source code and the
> resulting program. But that doesn't make must sense, either - as I
> recall, you've shown a considerably higher level of understanding in
> previous messages posted to this newsgroup; at least, they were posted
> using the same e-mail address.
>

I usually do. However, I tried to *over think* the problem. It
happens.

Chad