edits: 01/06/2008
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 area of the program in which the identifier may be used. All the variables in these puzzles have block scope.
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:
What does the following code write to the monitor?
#include <stdio.h> int main( void ) { int a = 1; int b = 2; printf("a=%d\tb=%d\n", a, b ); system("pause"); return 0; }
The two variables a
and b
are defined at the beginning
of a code block. They have block scope.
What does the following code write to the monitor?
#include <stdio.h> int main( void ) { int a = 1; int b = 2; { int b = 3; printf("a=%d\tb=%d\n", a, b ); } printf("a=%d\tb=%d\n", a, b ); system("pause"); return 0; }
This program has a nested scope inside the main scope. At the beginning of
that scope is a declaration of a variable, b
, which has block scope
for that block.
What does the following code write to the monitor?
#include <stdio.h> int main( void ) { int a = 1; int b = 2; printf("main : a=%d\tb=%d\n", a, b ); { int b = 3; printf("block1 : a=%d\tb=%d\n", a, b ); } { printf("block2s: a=%d\tb=%d\n", a, b ); int a = 4; printf("block2e: a=%d\tb=%d\n", a, b ); } printf("main : a=%d\tb=%d\n", a, b ); system("pause"); return 0; }
Here, there are two sequential inner blocks, each with a declaration of a variable. All variables in this program have block scope, but different variables have scope within different blocks.
Will the following program compile without errors?
#include <stdio.h> int main( void ) { int a = 1; int b = 2; { int b = 3; int c = 4; printf("a=%d\tb=%d\tc=%d\n", a, b, c ); } { int a = 4; printf("a=%d\tb=%d\tc=%d\n", a, b, c ); } system("pause"); return 0; }
Hint: consider the identifier c
.
What does the following code write to the monitor?
#include <stdio.h> int main( void ) { int a = 1; int b = 2; printf("scope m: a=%d\tb=%d\n", a, b ); { int b = 3; printf("scope 1: a=%d\tb=%d\n", a, b ); { int b = 4; printf("scope 2: a=%d\tb=%d\n", a, b ); } printf("scope 1: a=%d\tb=%d\n", a, b ); } printf("scope m: a=%d\tb=%d\n", a, b ); system("pause"); return 0; }
There are now three separate variables, each named b
, declared
in three nested blocks.
Will the following code compile?
#include <stdio.h> int main( void ) { int a = 1; int b = 2; if ( a == b ) { int c = 3; } else { c = 5; } system("pause"); return 0; }
What does the following code write to the monitor?
#include <stdio.h> int main( void ) { int a = 7; int b = 1; if ( a >= b ) { int temp = a; a = b; b = temp; } printf("a=%d\tb=%d \n", a, b ); system("pause"); return 0; }
Examine the following code:
#include <stdio.h> int main( void ) { int i ; { int b = 99; printf("b=%d\n", b ); } { int a; printf("a=%d\n", a ); } system("pause") ; return 0 ; }
Question 1: Is it predictable what this program will write to the monitor?
Question 2: What might the program write to the monitor, mostly as an accident?
What does the following code write to the monitor?
#include <stdio.h> int main( void ) { int a=1 ; printf("scope m: a=%d\n", a ); if ( a ) { int b = 2; printf("scope 1: a=%d\tb=%d\n", a, b ); if ( b ) { int c = 3; printf("scope 2: a=%d\tb=%d\tc=%d\n", a, b, c ); a = b = c = 99; printf("scope 2: a=%d\tb=%d\tc=%d\n", a, b, c ); } printf("scope 1: a=%d\tb=%d\n", a, b ); } printf("scope m: a=%d\n", a ); system("pause") ; return 0 ; }
This puzzle is not particularly tricky (like the last one). Just routine application of the rules will do.
What does the following code write to the monitor?
#include <stdio.h> int main( void ) { int a=1, b=2, c=3 ; printf("scope m: a=%d\tb=%d\tc=%d\n", a, b, c ); if ( a ) { int b = 66; printf("scope 1: a=%d\tb=%d\tc=%d\n", a, b, c ); if ( b ) { int c = 77; printf("scope 2: a=%d\tb=%d\tc=%d\n", a, b, c ); } printf("scope 1: a=%d\tb=%d\tc=%d\n", a, b, c ); } printf("scope m: a=%d\tb=%d\tc=%d\n", a, b, c ); system("pause") ; return 0 ; }
As with P09, this puzzle is not particularly tricky . Just routine application of the rules will do.