C Programming Homework Solutions: Variable Scope and Precedence, Assignments of Computer Science

Solutions and explanations for various c programming homework questions. Topics covered include variable scope, precedence of operators, and side-effects in for loops. The document also includes examples and points to ponder for further understanding.

Typology: Assignments

Pre 2010

Uploaded on 03/18/2009

koofers-user-ft2-1
koofers-user-ft2-1 ๐Ÿ‡บ๐Ÿ‡ธ

4

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Homework 2 Solution Guidelines
1. There are four int variables called i, one each in blocks 1, 2, 4, and 5. Each printf
statement refers to the variable called i that is visible from it โ€“ remember more specific
declarations โ€œhideโ€ less specific ones. So the first printf refers to the Block 1 i, the
second one (which is in Block 2), refers to the Block 2 i, the third prinitf (which is
between Blocks 2 and 3, and belongs in Block 1) refers to Block 1 i. The fourth one is in
Block 3, but Block 3 has not defined an i variable of its own, so this printf refers to the
Block 1 i, which is the only one visible to it.
The fifth printf refers to the Block 4 i, and the sixth refers to the Block 5 i defined just
above it. The seventh (last) one is outside all the other blocks, and refers to the Block 1
i.
One point to note is that the variable declaration after a line of source code in Block 5 is
an error according to older standards of C, but is now allowed. Older compilers may
refuse to compile this code โ€“ new ones will compile with a warning.
The wisdom of the warning is clear when trying to trace the output. The first statement in
Block 5 ends up adding to the value of the Block 1 i, not the Block 5 i (as might be
expected or might be the intent of the programmer). This is because when the compiler is
compiling that line, it does not know that a Block 5 i is going to be defined in the next
line. The only i visible to it at that point is the Block 1 i, which is what it uses.
Point to Ponder: What if Block 5 was a loop that ran a few times? What would be the
result of these statements? Try to reason about it before trying it out.
Finally, the output will be:
1 ๏ƒŸ Block 1 i
2 ๏ƒŸ Block 2 i
1 ๏ƒŸ Block 1 i
2 ๏ƒŸ Block 1 i
4 ๏ƒŸ Block 4 i
9 ๏ƒŸ Block 5 i
8 ๏ƒŸ Block 1 i
pf3
pf4

Partial preview of the text

Download C Programming Homework Solutions: Variable Scope and Precedence and more Assignments Computer Science in PDF only on Docsity!

Homework 2 Solution Guidelines

1. There are four int variables called i, one each in blocks 1, 2, 4, and 5. Each printf

statement refers to the variable called i that is visible from it โ€“ remember more specific

declarations โ€œhideโ€ less specific ones. So the first printf refers to the Block 1 i, the

second one (which is in Block 2), refers to the Block 2 i, the third prinitf (which is

between Blocks 2 and 3, and belongs in Block 1) refers to Block 1 i. The fourth one is in

Block 3, but Block 3 has not defined an i variable of its own, so this printf refers to the

Block 1 i, which is the only one visible to it.

The fifth printf refers to the Block 4 i, and the sixth refers to the Block 5 i defined just

above it. The seventh (last) one is outside all the other blocks, and refers to the Block 1

i.

One point to note is that the variable declaration after a line of source code in Block 5 is

an error according to older standards of C, but is now allowed. Older compilers may

refuse to compile this code โ€“ new ones will compile with a warning.

The wisdom of the warning is clear when trying to trace the output. The first statement in

Block 5 ends up adding to the value of the Block 1 i, not the Block 5 i (as might be

expected or might be the intent of the programmer). This is because when the compiler is

compiling that line, it does not know that a Block 5 i is going to be defined in the next

line. The only i visible to it at that point is the Block 1 i, which is what it uses.

Point to Ponder: What if Block 5 was a loop that ran a few times? What would be the

result of these statements? Try to reason about it before trying it out.

Finally, the output will be:

1 ๏ƒŸ Block 1 i

2 ๏ƒŸ Block 2 i

1 ๏ƒŸ Block 1 i

2 ๏ƒŸ Block 1 i

4 ๏ƒŸ Block 4 i

9 ๏ƒŸ Block 5 i

8 ๏ƒŸ Block 1 i

2. First of all, % has higher precedence than ==, so all the innermost brackets that enclose

expressions of the form (year % 4) can be removed without changing the order of

evaluation or the meaning. This gives us:

(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))

Next, the brackets that are now innermost serve to ensure that the == and != tests will be

done before anything else. But the only competitors are && and || , both of which have

lower precedence. So these brackets can be removed also, giving us:

((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)

The only bracket now left serves to ensure that the logical AND operation is done before

the logical OR condition. But && has higher precision than || anyway, so this bracket

can also be removed without changing anything. Now only the outer bracket is left,

which is obviously needed to make the if statement syntactically correct. So the final

answer is :

(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)

One point to note: on many compilers this last one will produce a warning, suggesting

that you put more brackets. This is strictly not the job of the compiler to do. The reason

is that many programmers are historically seen to forget that && and || have actually

different precedences, and assume that they have equal precedence with left-to-right

associativity, which will sometimes lead to errors. Hence this warning, which can be

ignored by anybody that knows exactly what they want to do, and is aware of the correct

rules.

3. These are explanations of what happen in the various cases. In cases that do not

produce an error, you can find the actual values printed by compiling and executing.

printf ("%d\n", i=j++);

compiles fine, results in setting the value of i to the value of j, and a post-increment is

applied to j.

printf ("%d\n", (i=j)++);

will not compile. The syntax indicates that (i=j) is to be incremented. However,

(i=j) is an assignment statement that returns a value , but there is lvalue to be

incremented here. That is, what variable is to be incremented? Remember, an

assignment expression returns the value that was assigned, not the variable to which the

value was assigned.

printf ("%d\n", (i==j)++);

will not compile, for a similar reason as above. Here the statement inside the brackets is

a logical test, and will return either 0 or 1 (in this case 0, because i is 6 and j is 10). This

is a value and not an lvalue, and cannot be incremented.

printf ("%d\n", i++ = j);

int i, j; i=1; j=0; for (; (i<1000;) { j = i; i = i + j; } printf ("First 2 - pow after 1000 is %d\n", i);

This trivializes the for to a while. One might say that the for statement is supported by

the language expressly to utilize side-effects and create compact code.