revised: 01/06/2008, 06/23/2017
These puzzles involve looking at code examples and predicting what will be written on the monitor. To do this, you need to figure out the scope of various variables.
The scope of an identifier is the section of the source file in which the identifier may be used.
C compilers start at the top of a source file and work down line by line until they reach the end.
This behavior one-pass compiling.
Because of this, an identifier definition must appear earlier in a source file than any statement that uses it.
(This is unlike Java and some other languages.)
The following would not compile because when the compiler reaches the assignment statement, it
has not seen a
.
int main() { a = 7; int a; }
The following would compile:
int main() { int a; a = 7; }
An identifier has block scope when it appears inside a block
or appears in the parameter list of a function definition.
Syntactically, a block consists of statements enclosed by left and
right braces, {
and }
.
An identifer with block scope follows these rules: