Docsity

Homework 4 Solution: Understanding Block Scope and Global Variables, Assignments of Computer Science

Toggle sidebar
bg1
Homework 4 Solution Guidelines
1. It will compile just fine, though it might give you warnings about implicit declaration
of printf, since stdio.h was not included.
The output is:
1
2
1
4
The first printout is from Block 1, the initialized value of the Block 1 i is printed. The
second is the Block 2 i, again the initialized value. The third is the printout from Block
3, which prints the Block 1 i, which is still 1. The last one is from Block 4, printing the
local Block 4 i, initialized to 4.
The last printf statement does not produce anything since it comes after the return
statement, and is never executed. Interestingly, this does not produce any warnings.
2. The output is:
1
2
3
4
1
Briefly, the printouts come from Block 1 (main), Block 2, Block 3, Block 4, and Block 1
respectively, and each refers to a Block local i.
3. Any time we replace a local definition of i with an extern statement which is a
declaration of i, effectively within that block we refer to the global i (which was
initialized to 1). The results, for the various cases, are:
(a) 1
2
pf2

Partial preview of the text

Download Homework 4 Solution: Understanding Block Scope and Global Variables and more Assignments Computer Science in PDF only on Docsity!

Homework 4 Solution Guidelines

  1. It will compile just fine, though it might give you warnings about implicit declaration of printf, since stdio.h was not included. The output is: 1 2 1 4 The first printout is from Block 1, the initialized value of the Block 1 i is printed. The second is the Block 2 i, again the initialized value. The third is the printout from Block 3, which prints the Block 1 i, which is still 1. The last one is from Block 4, printing the local Block 4 i, initialized to 4. The last printf statement does not produce anything since it comes after the return statement, and is never executed. Interestingly, this does not produce any warnings.
  2. The output is: 1 2 3 4 1 Briefly, the printouts come from Block 1 (main), Block 2, Block 3, Block 4, and Block 1 respectively, and each refers to a Block local i.
  3. Any time we replace a local definition of i with an extern statement which is a declaration of i, effectively within that block we refer to the global i (which was initialized to 1). The results, for the various cases, are: (a) 1 2

(b) 1 2 3 4 3 (c) 1 2 3 4 5 (d) 1 2 3 4 5