Long winded question about objects and identifiers

C

Chad

The questions are about the following lines of C code..


#include <stdio.h>

int x = 3;

int main(void)
{
printf("The value of x is: %d\n", x);

{
int x = 25;
printf("The value of x is now : %d\n", x);
}

printf("The value of x again is: %d\n", x);

return 0;
}

When I run it, I get....

The value of x is: 3
The value of x is now : 25
The value of x again is: 3

So what gets changed? The (value of the) object (named 'x')? Or does
'x' itself change? Also, would 'x' be considered an identifier?
 
B

bart.c

Chad said:
The questions are about the following lines of C code..


#include <stdio.h>

int x = 3;

int main(void)
{
printf("The value of x is: %d\n", x);

{
int x = 25;
printf("The value of x is now : %d\n", x);
}

printf("The value of x again is: %d\n", x);

return 0;
}

When I run it, I get....

The value of x is: 3
The value of x is now : 25
The value of x again is: 3

So what gets changed? The (value of the) object (named 'x')? Or does
'x' itself change? Also, would 'x' be considered an identifier?

You have two locations, one containing 25, the other 3. Both are called 'x'.

The name 'x' inside the inner {...} hides the one outside.

And yes 'x' is an identifier.
 
C

Chad

You have two locations, one containing 25, the other 3. Both are called 'x'.

The name 'x' inside the inner {...} hides the one outside.

And yes 'x' is an identifier.

So would each 'x' be distinct objects?
 
K

Keith Thompson

Chad said:
The questions are about the following lines of C code..

#include <stdio.h>

int x = 3;

int main(void)
{
printf("The value of x is: %d\n", x);

{
int x = 25;
printf("The value of x is now : %d\n", x);
}

printf("The value of x again is: %d\n", x);

return 0;
}

When I run it, I get....

The value of x is: 3
The value of x is now : 25
The value of x again is: 3

So what gets changed? The (value of the) object (named 'x')? Or does
'x' itself change? Also, would 'x' be considered an identifier?

You have two distinct objects, both named ``x''. The first, which we
can call the outer x, is created by the definition ``int x = 3;''.
It has static storage duration, meaning that it exists for the
entire lifetime of your executing program. The second, the inner
x, is created by the definition ``int x = 25;''. It has automatic
storage duration, meaning that it exists only during the execution
of the block (compound statement) that contains its definition.

When the first and third printf calls are executed, only the outer
x exists. When the second printf call is executed, both objects
exist simultaneously, but only the inner one is visible, because
it hides the outer one.

The only thing these two objects have in common is that they both
have the same name and the same type. You could change the name
of the inner x to y, and the program would do exactly the same thing.

Yes, x is an identifier.

This kind of hiding is usually not a good idea, because it can cause
confusion.
 
A

August Karlstrom

Keith Thompson wrote:
[...]
This kind of hiding is usually not a good idea, because it can cause
confusion.

That's why some languages do not permit it. Eiffel comes to mind.


August
 
N

Nick

Chad said:
The questions are about the following lines of C code..


#include <stdio.h>

int x = 3;

int main(void)
{
printf("The value of x is: %d\n", x);

{
int x = 25;
printf("The value of x is now : %d\n", x);
}

printf("The value of x again is: %d\n", x);

return 0;
}

When I run it, I get....

The value of x is: 3
The value of x is now : 25
The value of x again is: 3

So what gets changed? The (value of the) object (named 'x')? Or does
'x' itself change? Also, would 'x' be considered an identifier?

Remember, C is a compiled[*] language. By the time you execute your
program there is (apart from debugging information) no 'x' anywhere to
be seen.

So when the compiler sees "int x=3" it writes code to create a bit of space
for an integer and puts 3 into it, and inside the compiler's workspace
it notes that x is associated with that address.

Then when it comes to the printf it looks up 'x' in that internal
workspace and finds the address and writes code to print whatever is at
that address (a clever compiler might notice that x can't change and
just put an actual 3 in there, but that's not important here).

When the compiler hits the block instruction '{' it notes that new
variables can exist. So when it now sees "int x=25" it writes code to
create some space for an integer and puts 25 into it and - in some way
(and there are lots of ways of doing it) changes its internal workspace
so that 'x' is now associated with that new address.

So as before, it creates code to use the appropriate address when you
print x.

Then, at the end-of-block '}' it removes all the references created
inside the block, and restores old ones, so x now refers to the original
space and the later printf gives the 3 again.

[*] - yes, I know there have been interpreters, but almost everything is
a compiler now, and the explanation would have been even more confusing
if I'd tried to address both at once.
 
E

Eric Sosman

Yes, each x (global and local) designate distinct objects.

Might an analogy help? Chad, try answering these:

- Is "Chad" an identifier?

- Does "Chad" identify one and only one person, or is
there more than one "Chad" in the world?

- If there is more than one "Chad," are they distinct
people or are they all the same person?

- If there is more than one "Chad," do they all share the
same characteristics? (Same nationality, same age, same
parentage, ...) That is, can they have different "types"
and different "values," or are they all the same?

- If there is more than one "Chad," how can someone decide
which of them is "The particular Chad we're talking about?"

Now rewrite all the questions, substituting "x" for "Chad,"
"thing" for "person," "program" for "world," "referring to" for
"talking about," and so on, and answer the rewritten questions.
Do the answers seem familiar?
 
W

William Hughes

The questions are about the following lines of C code..

#include <stdio.h>

int x = 3;

int main(void)
{
  printf("The value of x is: %d\n", x);

  {
    int x = 25;
    printf("The value of x is now : %d\n", x);
  }

  printf("The value of x again is: %d\n", x);

  return 0;

}

When I run it, I get....

The value of x is: 3
The value of x is now : 25
The value of x again is: 3

So what gets changed? The (value of the) object (named 'x')? Or does
'x' itself change? Also, would 'x' be considered an identifier?

The fact that you can do something like

#include <stdio.h>

int x = 3;

int main(void)
{
int *old_x;
old_x = &x;
printf("The value of x is: %d\n", x);

{
int x = 25;
printf("The value of x is now : %d\n", x);
printf("The value of x was : %d\n", *old_x);
}

printf("The value of x again is: %d\n", x);

return 0;

}

suggests that there must be two objects and which
object is associated with the identifier x can change.

However, there is more than one way to achieve the
observed result, and the implementation is free to
use any method which achieves the observed result
(the "as if" rule) so we can't be sure.
I cant think of any good way in which you could have
x always associated with the same storage
but there may be one. I can certainly think of some
horrible ways it could be done.

-William Hughes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top