Section 1.6 Exercises

Answers to this section have been verified with gcc on Cygwin. If you’re keeping score at home, I got the answers right before checking them.

1. For the following block of C code, indicate the values assigned to w, x, y and z.

int w,x,y,z;

int i = 4;
int j = 5;
{
    int j = 7;
    i = 6;
    w = i + j;
}
x = i + j;
{
    int i = 8;
    y = i + j;
}
z = i + j;

Answer:

w = 13, x  = 11, y = 13, z = 11

2. As above, for following:

int w, x, y, z;
int i = 3; int j = 4;
{ 
    int i = 5;
    w = i + j;
}
x = i + j;
{
    int j = 6;
    i = 7;
    y = i + j;
}
z = i + j;

Answer: w = 9, x = 7, y = 13, z = 11

3. is excessively academic

4. What is printed by the following C code?

#define a (x + 1)
int x = 2;
void b() { x = a; printf("%d\n", x); }
void c() { int x = 1; printf("%d\n", a); }
void main() { b(); c(); }

Answer:

“3

2”

I learned some things in this chapter. Mostly foundational definitions – very specific explanations of the meaning of static vs dynamic scope – basically the difference is between knowing the location associated with a name at compile time vs. at runtime – and the explicit meanings of variables, names, identifiers, and locations – a variable refers to an actual location, an identifier references an identity, but can be a sort of subidentity of something else – for example, in “foo.bar,” “foo” and “bar” are identifiers, and “foo.bar” is a name but not an identifier. I think this is because the ‘.’ is a special character that relates identifiers within a name – apparently I’m still not as clear on this as I should be.

An interesting point is made about static/dynamic scope – “The static rule asks us to find the declaration whose unit (block) most closely surrounds the space of the name; the dynamic rule asks us to find the declaration whose unit (procedure invocation) mostly closely surrounds the time of the call to the name” (heavily paraphrased)

This section, overall, got far too pedantic for what it’s actually attempting to cover, going into excruciatingly specific verbiage using phrases like “Let B1, B2, … Bk be all the blocks that surround the use of the name x, with Bk the smallest, nested within Bk-1.” This is perfectly sensible (and painstakingly precise), but it’s unnecessarily obtuse – the concept is very simple and anyone who’s done a year or two of programming knows it whether or not they’re comfortable following the notation. I don’t mind it so far, but I’m concerned that when I get to concepts that I don’t already understand, the pedantry will weigh on me and make it difficult to proceed.

Stay tuned!

Advertisement
This entry was posted in Dragon book. Bookmark the permalink.

2 Responses to Section 1.6 Exercises

  1. Stephen Corwin says:

    Note sure it matters to you, but you must have forgotten a line in Ex. 1, because z never gets a value at all.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s